Pure Programmer
Blue Matrix


Cluster Map

Project: Array Last Element

Write a program that declares an integer array and initializes it with a number of values. Then have the program print out just the last element of the array. You can not hard code the index of the last element but must determine it dynamically from the array itself. Try changing the array initializer to test out arrays of differing lengths.

Output
$ rustc ArrayLastElement.rs error[E0308]: mismatched types --> ArrayLastElement.rs:10:24 | 10 | let valueSize:isize = values.len(); | ----- ^^^^^^^^^^^^ expected `isize`, found `usize` | | | expected due to this | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 10 | let valueSize:isize = values.len().try_into().unwrap(); | ++++++++++++++++++++ error[E0277]: the type `[isize]` cannot be indexed by `isize` --> ArrayLastElement.rs:12:41 | 12 | println!("Last element is: {}", values[valueSize - 1]); | ^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error: aborting due to 2 previous errors Some errors have detailed explanations: E0277, E0308. For more information about an error, try `rustc --explain E0277`.

Solution