Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Collections

In this exercise, you will learn how to work with Rust standard collections. The documentation of the std::collections is excellent and provides an overview of the most important containers which are also included in the standard library.

You will learn to:

  • Read user input from the terminal.
  • Use the Rust std::vec::Vec dynamic array container.
  • Sort sequences.
  • Use the Rust std::collections::BTreeSet sorted set container.
  • Use the std::collections::HashMap map container.

You can find the solutions here.

Task

  1. Create a new binary crate called collections
  2. Read in a string of space separated words from Standard Input and place them into a vector. Then sort the vector and print the sorted list.
  3. Repeat step 2, but use the std::collections::BTreeSet container instead which keeps the elements in sorted order. Print the list again.
  4. Create a second binary inside the package by creating a bin folder inside the src folder and creating a new map.rs file inside it.
  5. Read in a string with the syntax key1:value1,key2:value2 and store the key-value pairs in a std::collections::HashMap. A nice solution also deals with extra whitespace, e.g. key1: value1, key2: value2.

Hints

These hints can help you with completing individual steps.

Reading input from the console

The std::io module contains everything required to read from the console or write to the console. The operating system models those using pre-defined standard streams. The Standard In, Out and Error streams are exposed by the std::io::stdin, std::io::stdout and std::io::stderr functions.

The std::io::Stdin handle implements the std::io::Read trait and also exposes the read_line method to help with this task. This function expects a mutable reference to a String, so you need to create an empty one before calling it.

Code to read a line from the console:

#![allow(unused)]
fn main() {
    let mut input_string = std::string::String::new();
    std::io::stdin().read_line(&mut input_string).expect("reading input string failed");
}

Word extraction

Rust provides useful APIs for string manipulation and extraction. You might find the following APIs useful:

Sorting a vector

The Vec data structure has a lot of useful methods, including sorters.

You can use both the sort and the sort_unstable method which sort in-place and do not require allocation.

Quiz

After you have worked through this exercise, you can try this quiz to double check your understanding:

Q1:

How does the sort method or the BTreeSet know how to order elements? Why did it just magically know how to sort our strings/words?

Hint:

Have a look at the sort API or the BTreeSet docs.

Answer:

A:

All APIs or data structures which do sorting rely on a core::cmp::Ord implementation of whatever type you are sorting. This trait is already implemented by commonly used standard library types like str and String.

Q2:

What is the advantage of using &str over String?

Hint:

Check the documentation of String. How/Where is the string stored in memory?

Answer:

A:

String allocates on the heap and creates a copy of the word that was read in. It is more expensive than a simple &str reference.