The title of this question is probably misleading and not completely relevant. I'm new to this language and believe I'm banging my head against the "borrow checker' and I should fix in my mental mdoel of how Rust deals with ownership, among other things. I may break the one-question-per-question rule in this post.
Here is some incomplete code to build an adjacency list:
type AdjList = HashMap<i32, Vec<i32>>;
fn adj_list(elist: Vec<(i32, i32)>) -> AdjList {
let mut adjacency_list = AdjList::new();
for i in elist.iter() {
if adjacency_list.contains_key(&i.0) {
adjacency_list.get(&i.0).unwrap().push(i.1);
} else {
let v: Vec<i32> = Vec::new();
adjacency_list.insert(i.0, v);
}
}
adjacency_list
}
A few things of interest are going on here. When I create the adjacency list, I do so by calling the new()
constructor on AdjList
since AdjList
is just a type alias to a HashMap
with it's concrete types already filled in. I attempt to make the AdjList
named adjacency_list
mutable by using the mut
keyword. However, when I attempt to push an element into the vector of integers in this map the compiler says
cannot borrow immutable borrowed content as mutable
How do I make the vector contained in the AdjList
, or HashMap
, mutable? Why does the mut
keyword make the HashMap
mutable but not a vector it contains?
Another thing of interest is what happens when one constructs an AdjList
. Can someone explain why calling adjacency_list.get( &i.0 )
returns an Option type instead of an empty Vec<i32>
?
Thanks for reading