In the first example, you return &Tand reference something, so the value and types correspond to:
fn last<T: Clone>(slice: &[T]) -> &T {
&slice[slice.len()-1]
}
But, then you said that you were no longer going to return the link, but did not change the implementation.
fn last<T: Clone>(slice: &[T]) -> T {
&slice[slice.len()-1]
}
T, &Tand &mut Tall different types from each other! This means that this is the same as this “little setup”:
fn foo() -> i32 { 42 }
fn foo() -> bool { 42 }
& :
fn last<T: Clone>(slice: &[T]) -> T {
slice[slice.len()-1]
}
...
error[E0507]: cannot move out of indexed content
--> src/main.rs:4:9
|
4 | slice[slice.len()-1]
| ^^^^^^^^^^^^^^^^^^^^ cannot move out of indexed content
" " ? ?.
: . :