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::Vecdynamic array container. - Sort sequences.
- Use the Rust
std::collections::BTreeSetsorted set container. - Use the
std::collections::HashMapmap container.
You can find the solutions here.
Task
- Create a new binary crate called
collections - 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.
- Repeat step 2, but use the
std::collections::BTreeSetcontainer instead which keeps the elements in sorted order. Print the list again. - Create a second binary inside the package by creating a
binfolder inside thesrcfolder and creating a newmap.rsfile inside it. - Read in a string with the syntax
key1:value1,key2:value2and store the key-value pairs in astd::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:
splitand the variantssplit_onceandsplit_whitespacetrim
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.