1

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
GeckStar
  • 1,136
  • 1
  • 14
  • 22
  • 1
    @Shepmaster this code has a different problem than the code in your duplicate. – Lukas Kalbertodt Oct 18 '16 at 19:44
  • @LukasKalbertodt it's true, but I'm answering (duplicating) based on the question asked in the title. Trying to solving the OPs problem, as I don't suppose they want to hear the direct answer: "All blocks in an `if` statement must return the same type, and `Vec::push` and `HashMap::insert` don't have the same type". – Shepmaster Oct 18 '16 at 19:44
  • 2
    If that's what OP is interested in, this can be a duplicate of http://stackoverflow.com/q/24579756/155423; http://stackoverflow.com/q/37554325/155423; http://stackoverflow.com/q/24502282/155423; http://stackoverflow.com/q/39522242/155423; or potentially others. – Shepmaster Oct 18 '16 at 19:50
  • @GeckStar: does this help? I really want to know if all of this is actually helpful to you or not. And if not, how you would expect/hope this question is handled? – Lukas Kalbertodt Oct 18 '16 at 20:03
  • I'm currently checking out the duplicate, which means that I'm looking into how to use `Entry`. The code snippets from the other duplicates in Shepmaster's second comment gave me more ideas on what to use if I fail to get it right with `Entry`, e.g. using `if`clauses with `map.contains_key(key)`. The best answer would have been to suggest an alternative to the match construct in my answer imo, since I'm understand from Shepmaster's first comment that I'm trying something here that is inherently flawed, that is using statements returning different types within a `match` block. – GeckStar Oct 18 '16 at 20:12
  • 1
    I hope you don't read too much negativity into my duplicate closing. Like @LukasKalbertodt says, we *do* want to answer questions, but I don't want to rewrite the same answers for every specific permutation of submitted code. – Shepmaster Oct 18 '16 at 20:18
  • FWIW, you **cannot** use the `match` or `if` form combined with `get` because of borrowing issues. That's the *reason* that the `Entry` API was created, so that entire direction would be a dead end. There's also issues with immutable vs mutable borrowing, moving out out borrowed content, etc. – Shepmaster Oct 18 '16 at 20:21
  • 1
    Gotcha. I didn't read any negativity into your duplicate closing, if anything that just showed me that I didn't search thoroughly enough for the problem. I have only started learning Rust this week though, so I'm currently lacking the skill to put the problems I'm facing into words, which makes it hard to search for this stuff. – GeckStar Oct 18 '16 at 20:35

0 Answers0