I wrote a struct Edge which represents a tuple of points, given by IDs as u16 types.
#[derive(Debug)]
struct Edge {
left: u16,
right: u16,
}
In my main function, I have a Vector of edges and a HashMap which maps every point ID to a Vector of all edges of which it is the left object.
To fill the HashMap, I iterate over the Vector of edges, check if the left value of the edge is already a key in the HashMap, and if so, I simply add the edge to the Vector of edges, and if not, I insert the new key and value.
let mut map = HashMap::<u16, Vec<Edge>>::new();
let edge_list: Vec<Edge> = vec![]; // emptiness of this vector should not affect the code
for edge in edge_list.iter() {
match map.get(&edge.left) {
Some(x) => x.push(*edge),
None => map.insert(edge.left, vec![*edge]),
}
}
However, the compiler does not like my code:
error[E0308]: match arms have incompatible types
--> src/main.rs:14:9
|
14 | match map.get(&edge.left) {
| ^ expected (), found enum `std::option::Option`
|
= note: expected type `()`
= note: found type `std::option::Option<std::vec::Vec<Edge>>`
note: match arm with an incompatible type
--> src/main.rs:16:21
|
16 | None => map.insert(edge.left, vec![*edge]),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I don't know what it is expecting. map.get(...) should return an Option, i.e. it is either Some(x) or None, and I am evaluating both cases. I have seen a bunch of cases where match was used to deconstruct an Option. Why would match expect ()? What could be wrong?