Rustのentry APIは、マップ内の1つのエントリへのビューを提供します。マップはHashMapまたはBTreeMapのいずれかです。
エントリは、値が存在する状態か、存在しない状態のどちらかで、APIはエントリの内容を変更するためのメソッドを提供します。
この演習では、HashMapとentry APIのメソッドを使って、簡単なアルゴリズムの問題を解きます。
雑誌の記事の単語を表す&[&str]と、送りたいメモの単語を表す&[&str]が与えられたとき、雑誌から単語を切り取って手紙に貼り付け、メモを組み立てられるでしょうか?
補足:
まずは、次のスタブの関数シグネチャから始めます。
pub fn can_construct_note(magazine: &[&str], note: &[&str]) -> bool {
todo!()
}
次の入力を与えると
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));
この関数はfalseを返します。メモには"two"が2つ必要ですが、雑誌には"two"が1つしか含まれていないからです。
次の入力では成功します。
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));
この関数はtrueを返します。メモに必要な単語がすべて雑誌に含まれているからです。