I'm learning Rust with the official book. I've come across a weird syntax in my program:
pub struct Shelf<'a> {
items: Vec<&'a Item<'a>>, // => working as expected
//items: Vec<Item<'a>>, // => not working
//items: Vec<&'a Item>, // => not working
}
Item is a struct that contains references to other types too:
pub struct Item<'a> {
owner: &'a Owner,
name: String,
avg_rating: u32,
status: ItemStatus,
}
pub struct Owner {
pub name: String,
}
It seems to me that the syntax items: Vec<&'a Item<'a>>
is weird and I don't think I'm doing right... What I want is a Vec
that contains references to Item
s, and that Vec
is valid as long as the references to Item
s it contains are themselves valid. Shouldn't it be items: Vec<&'a Item>
instead?