-4

I asked a question a few minutes ago about how to get a reference to the root node of a Rust BTreeMap. I have since learned that this can be done using first_entry which is a function which returns an Option<OccupiedEntry> object.

However, I cannot figure out how to map that value to the underlying type.

I thought this might work:

let mut map = BTreeMap::new();
map.insert("A", 5);
map.insert("B", 6);
map.insert("C", 3);

let optional_occupied_entry = map.first_entry();

let f = |x: OccupiedEntry<T, usize>| {
    x.get()
};

let optional_value = optional_occupied_entry.map(f);

This is the compiler error I am encountering:

error[E0631]: type mismatch in closure arguments
    --> XXX/src/lib.rs:58:29
     |
47   |             let f2 = |x: std::collections::btree_map::OccupiedEntry<T, usize>| {
     |                      --------------------------------------------------------- found signature defined here
...
58   |             let m1 = m1.map(f2);
     |                         --- ^^ expected due to this
     |                         |
     |                         required by a bound introduced by this call
     |
     = note: expected closure signature `fn(std::collections::btree_map::OccupiedEntry<'_, Reverse<T>, _, _>) -> _`
                found closure signature `for<'a> fn(std::collections::btree_map::OccupiedEntry<'a, T, _, _>) -> _`
note: required by a bound in `Option::<T>::map`
    --> /home/XXX/.rustup/toolchains/1.70-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:1095:12
     |
1095 |         F: ~const FnOnce(T) -> U,
     |            ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Option::<T>::map`
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • 4
    [ask] says "Before posting a question [...] spend a reasonable amount of time researching" do you think less than 14 minutes is a reasonable amount of time researching? – cafce25 Aug 22 '23 at 10:32

1 Answers1

0

The correct way, as I explained in your previous question, is to use first_key_value().

If you need to modify the entry, use OccupiedEntry::into_mut() instead of get() and inline the closure:

let optional_value = optional_occupied_entry.map(|x| x.into_mut());

The change to into_mut() is required because get() borrows the entry while into_mut() consumes it, and the inline for the closure is required because of the quirks of type inference in closures.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77