In Rust, I have seen that we can use the .last()
function to access the last element of a vector. However, when I try to interpolate this into print output like so
fn main() {
let mut v: Vec<f64> = Vec::new();
v.push(1.0); v.push(1.9); v.push(1.2);
println!("Last element is {}", v.last()); // (*)
}
I get the following error message:
error[E0277]: `Option<&f64>` doesn't implement `std::fmt::Display`
The full error text hints that I can use {:?}
instead. If I change (*)
to read println!("Last element is {:?}", v.last()); // (*)
, then the program prints
Last element is Some(1.2)
How can I just get Last element is 1.2
?
How can I extract the value 1.2
as an f64
?