Hopefully the title is accurate.
I would like to set a field on the Node struct that is inside of a vector. The node is a mutable reference, so I'm not sure why I can't assign to it. I'm guessing I am not properly unwrapping the Option?
Code example:
#[derive(Debug)]
enum ContentType {
Big,
Small,
}
#[derive(Debug)]
struct Node {
content_type: Option<ContentType>
}
#[derive(Debug)]
struct List {
nodes: Vec<Node>,
}
impl List {
fn get_node(&self, index: usize) -> Option<&Node> {
return self.nodes.get(index);
}
}
fn main() {
let list = List {
nodes: vec![Node {content_type: None}]
};
let node = &mut list.get_node(0);
println!("{:?}", list);
if let Some(x) = node {
x.content_type = Some(ContentType::Big)
}
}
Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=18bdaf8b903d57dfbf49ebfb3252cf34
Receiving this error:
cannot assign to `x.content_type` which is behind a `&` reference