Can someone explain how this code is legal? It seems the &mut buffer is converted to io::Write instance (owned?) and not a reference here when passed from main to write_buffer. Does ownership with traits work differently? Also dropping the io::Write instance buffer in the method does not have any impact (seems logical as its just a mutable reference and not actually an owned value. But from syntax, it looks like an owned value. Is there a different syntax to move values and have trait bounds on such value?
#![allow(unused_variables)]
use std::io;
fn write_buffer<T>(buffer: T) where T: io::Write {
// let data: Vec<u8> = vec![1, 2, 3];
// buffer.write(data.as_slice());
drop(buffer);
}
fn main() {
let mut buffer: Vec<u8> = vec![1];
println!("{:?}", buffer);
write_buffer(&mut buffer);
println!("{:?}", buffer);
}
Output:
[1]
[1]