I have an fn that looks something like this:
use std::io::Read;
fn read_something(file: &mut impl Read) {
let _ = file.read(&mut [0; 8]);
}
When I pass a slice of a Vec into it like that:
fn main() {
let vector = vec![1, 2, 3, 4];
read_something(&mut &vector[..]);
}
it works fine. But when I save the slice into a variable first it does not compile:
fn main() {
let vector = vec![1, 2, 3, 4];
let slice = &vector[..];
read_something(&mut slice);
}
The compiler tells me, that I
cannot borrow as mutable
How do these two cases differ?
Why does the first example work even though the vector is not declared as mutable either?
How can I pass a &[u8] slice into that fn, that eats a &mut impl Read?