So what I am asking is, what is the difference between the return types, &std::vec::Vec and std::vec::Vec? Just curious. If I make a reference to something, I'm not creating a new type. It still retains its data and structure and so retains its type. But for some reason I get this error:
error[E0308]: mismatched types
--> src/cam.rs:170:3
|
168 | pub fn index2d(self, x: usize, y: usize) -> Vec<u8> {
| ------- expected `std::vec::Vec<u8>` because of return type
169 | let c = &self.pyxels[y*WIDTH+x];
170 | c
| ^
| |
| expected struct `std::vec::Vec`, found reference
| help: try using a conversion method: `c.to_vec()`
|
= note: expected type `std::vec::Vec<u8>`
found type `&std::vec::Vec<u8>`
^ (umm excuse me?)
That one little symbol (&) really seems to make all the difference and I have no clue why.
If I make a reference to something, I'm not creating a new type.
If by "creating a new type" , you mean "creating an object of a different type" , then yes, that's exactly what you're doing. A reference to a thing is not that thing. It's similar to the difference between having a house, and having a slip of paper with the address of a house written on it.
Though the syntax of Rust often makes access to an object through a reference look identical to direct access to that object.
let v: Vec<u8> = Vec::new();
let vr = &v;
assert_eq!(v.len(), rv.len());
So perhaps that is why you are confused?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.