I'm trying to help a colleague, but we're all a bit stumped on this one. Here's the reproducer.
The original code uses HashMap. They can't use the entry function because the retrieving the value may fail.
I'm trying to help a colleague, but we're all a bit stumped on this one. Here's the reproducer.
The original code uses HashMap. They can't use the entry function because the retrieving the value may fail.
You just stumbled on a limitation of the current borrow checker. Polonius, the new borrow checker, should fix these kind of errors (see Polonius update | Inside Rust Blog for a bit of background and some recent updates on the milestones). For now you can emulate it with the polonius-the-crab
crate
I think this is another example for polonius, see the book for details:
https://rust-lang.github.io/polonius/
for a solution on current version, see the crate polonius_the_crab
for this particular example, you can use HashSet::contains()
to check the existence, or you can use the nightly HashSet::get_or_insert_with()
API as a workaround, see .
if !foo.contains(key) {
foo.insert(key.to_string());
}
foo.get(key).unwrap()
or
foo.get_or_insert_with(key, || key.to_string())
Here's the github issue (or perhaps one of the related ones), in case you want to follow it.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.