]> git.lizzy.rs Git - rust.git/commit
Rollup merge of #82764 - m-ou-se:map-try-insert, r=Amanieu
authorMara <m-ou.se@m-ou.se>
Fri, 5 Mar 2021 09:57:22 +0000 (10:57 +0100)
committerGitHub <noreply@github.com>
Fri, 5 Mar 2021 09:57:22 +0000 (10:57 +0100)
commit232caad39581118619c5168284b15457d1c72900
treed29df8018252297932379b977d51fbe70a18cca4
parent68f2934a154abac9a2af72c55e4c08277172e087
parenteddd4f05012ed9ebefa6e2e5ca91da4116d30996
Rollup merge of #82764 - m-ou-se:map-try-insert, r=Amanieu

Add {BTreeMap,HashMap}::try_insert

`{BTreeMap,HashMap}::insert(key, new_val)` returns `Some(old_val)` if the key was already in the map. It's often useful to assert no duplicate values are inserted.

We experimented with `map.insert(key, val).unwrap_none()` (https://github.com/rust-lang/rust/issues/62633), but decided that that's not the kind of method we'd like to have on `Option`s.

`insert` always succeeds because it replaces the old value if it exists. One could argue that `insert()` is never the right method for panicking on duplicates, since already handles that case by replacing the value, only allowing you to panic after that already happened.

This PR adds a `try_insert` method that instead returns a `Result::Err` when the key already exists. This error contains both the `OccupiedEntry` and the value that was supposed to be inserted. This means that unwrapping that result gives more context:
```rust
    map.insert(10, "world").unwrap_none();
    // thread 'main' panicked at 'called `Option::unwrap_none()` on a `Some` value: "hello"', src/main.rs:8:29
```

```rust
    map.try_insert(10, "world").unwrap();
    // thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value:
    // OccupiedError { key: 10, old_value: "hello", new_value: "world" }', src/main.rs:6:33
```

It also allows handling the failure in any other way, as you have full access to the `OccupiedEntry` and the value.

`try_insert` returns a reference to the value in case of success, making it an alternative to `.entry(key).or_insert(value)`.

r? ```@Amanieu```

Fixes https://github.com/rust-lang/rfcs/issues/3092
library/std/src/lib.rs