Rust's entry API provides a view into a single entry in map, which may be either a HashMap
or a BTreeMap
.
The entry may be either occupied or vacant, and the API provides methods to modify the contents of the entry.
In this exercise you'll be using a HashMap
, along with entry API methods, to solve a simple algorithm problem.
Given &[&str]
representing the words of a magazine article, and &[&str]
representing the words of a note you would like to send, can you compose your note by cutting words out of the magazine and pasting them into a letter?
Notes:
You'll start with the following stubbed function signature:
pub fn can_construct_note(magazine: &[&str], note: &[&str]) -> bool {
todo!()
}
Given the following input
let magazine = "two times three is not four".split_whitespace().collect::<Vec<&str>>();
let note = "two times two is four".split_whitespace().collect::<Vec<&str>>();
assert!(!can_construct_note(&magazine, ¬e));
The function returns false
since the magazine only contains one instance of "two"
when the note requires two of them.
The following input will succeed:
let magazine = "Astronomer Amy Mainzer spent hours chatting with Leonardo DiCaprio for Netflix's 'Don't Look Up'".split_whitespace().collect::<Vec<&str>>();
let note = "Amy Mainzer chatting with Leonardo DiCaprio"
.split_whitespace()
.collect::<Vec<&str>>();
assert!(can_construct_note(&magazine, ¬e));
The function returns true
since the magazine contains all the words that the note requires.
Sign up to Exercism to learn and master Rust with 98 exercises, and real human mentoring, all for free.