Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The BTreeMap is a B-tree data structure that stores key/value pairs in an ordered way. See ProgramΒ 1. The main features of a BTreeMap are:

                     β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
                     β”‚10/"a"β”‚16/"r"β”‚23/"z"β”‚      β”‚
                     β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜
                    β•±               β•²      β•²
                   β•±                 β•²      β•²
β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”         ▏     β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
β”‚ 2/"t"β”‚ 5/"e"β”‚      β”‚      β”‚         ▏     β”‚25/"w"β”‚29/"o"β”‚      β”‚      β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜         ▏     β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜
                        β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
                        β”‚17/"w"β”‚19/"o"β”‚21/"j"β”‚22/"v"β”‚
                        β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜

ProgramΒ 1:A BTreeMap with integer keys and string values, using a maximum of 4 keys per node.

The main methods of the BTreeMap are presented in TableΒ 1.

TableΒ 1:Some methods of BTreeMap

Method

Description

append(&other)

Moves the elements of another map into this map.

clear()

Removes all elements.

contains_key(k)

Returns true if this map contains the key k.

entry(k)

Gives access to key/value pair.

get(k)

Returns a reference to the value, if any. The returned type is Option<&V>.

insert(k, v)

Inserts a new key/value pair.

is_empty()

Returns true if this map contains no elements.

iter()

Returns an iterator on all key/value pairs.

keys()

Returns an iterator over the keys. Order is arbitrary.

len()

Returns the number of elements.

new()

Creates a new map.

range(r)

Returns a double-ended iterator over a range of elements in the map.

remove(k)

Removes the specified key from the map.

split_off(k)

Splits the map into two at the given key. The current map is modified to contain all elements with a key lower than the given key. Remaining elements are returned into a new map.

values()

Returns an iterator over the values. Order is arbitrary.

29.1.1Creating a new instanceΒΆ

To create a new hash map BTreeMap<K, V>, an easy way, like for the Vec type, is to let the compiler infer the type. Inserting at least one key/value pair will give the compiler the information it needs:

Couldn't automatically determine type of variable `scores`.
Please give it an explicit type.

29.1.2Getting a valueΒΆ

Getting a value from the map is done using the get() method, which returns an Option<V>:

BTreeMap implements the Index trait, and thus provides the indexing operator ([]) for retrieving a value. Example:

29.1.3IteratingΒΆ

Using a for loop we can loop onto the key/value pairs:

29.1.4Replacing an existing valueΒΆ

Inserting a key/value pair, replaces the eventual existing key:

29.1.5Adding a value if no key presentΒΆ

The or_insert() of the Entry structure, allows to insert a key/value only if the key is not already defined:

29.1.6Updating an existing valueΒΆ

We suppose we want to increment the score of a team. We retrieve a reference on the current score of team "Red", using the or_insert() method in case the team is not declared, and increment it by dereferencing it with the * operator. The or_insert() method returns a &mut V:

Here is the result: