0
use std::collections::{HashMap, HashSet};

type Snapshot = HashMap<String, HashSet<String>>;

fn compare_snapshots(snapshot1: Snapshot, snapshot2: Snapshot, entry1: String, entry2: String) {}

fn main() {
    let snapshot1 = HashMap::new();
    let snapshot2 = HashMap::new();

    let entries1: HashSet<String> = snapshot1.keys().cloned().collect();
    let entries2: HashSet<String> = snapshot2.keys().cloned().collect();
    let entries: Vec<String> = entries1.union(&entries2).cloned().collect();

    for e1 in entries {
        for e2 in entries {
            if e1 < e2 {
                compare_snapshots(snapshot1, snapshot2, *e1.to_string(), *e2.to_string());
            }
        }
    }
}

And the following errors:

error[E0308]: mismatched types
  --> src/main.rs:18:57
   |
18 |                 compare_snapshots(snapshot1, snapshot2, *e1.to_string(), *e2.to_string());
   |                                                         ^^^^^^^^^^^^^^^ expected struct `std::string::String`, found str
   |
   = note: expected type `std::string::String`
              found type `str`

error[E0308]: mismatched types
  --> src/main.rs:18:74
   |
18 |                 compare_snapshots(snapshot1, snapshot2, *e1.to_string(), *e2.to_string());
   |                                                                          ^^^^^^^^^^^^^^^ expected struct `std::string::String`, found str
   |
   = note: expected type `std::string::String`
              found type `str`

Why did I get them given that I explicitly made gave entries a Vec<String> type?

Shepmaster
  • 274,917
  • 47
  • 731
  • 969
d33tah
  • 8,728
  • 10
  • 51
  • 128

1 Answers1

1

Your case can be reduced to

fn repro(_: String) {}

fn main() {
    repro(*"".to_string());
}

For some reason, you are dereferencing the String. String implements Deref<Target = str>, so you've explicitly attempted to make a str. This ultimately won't work because a str is unsized, but you are hitting the type mismatch first.

Remove the asterisk:

fn repro(_: String) {}

fn main() {
    repro("".to_string());
}

See also:

Shepmaster
  • 274,917
  • 47
  • 731
  • 969
  • Thanks, that answers half of the question. Now I need to figure out how to implement the cartesian product in a borrow-friendly manner... – d33tah Jun 07 '19 at 15:50
  • I read up a bit and iterating over the reference solved the problem. Thanks again. – d33tah Jun 07 '19 at 15:55
  • @d33tah https://docs.rs/itertools/0.8.0/itertools/trait.Itertools.html#method.cartesian_product – Shepmaster Jun 07 '19 at 16:09