Rustlings
RustBookだけだと基礎力が不安なことに気づいたので、これをやる。
ghq get -b 5.1.1 --shallow https://github.com/rust-lang/rustlings
cd <rustlings path>
cargo install --force --path .
rustlings watch
とした状態で、エディタを立ち上げてファイルを編集していく。
進め方としては、 rustlings watch
が順番にコンパイルエラーを指摘してくれるから、その順番にファイルを修正していくと理解。
コンパイルが通ったら褒めてくれる。
気がすむまで該当コードをいじったら、
// I AM NOT DONE
のコメント行を消すことで rustlings watch
が次のコンパイルエラーを指摘してくれる。
const
の存在を忘れていた。 const
ですむところは const
優先。
vec!["hoge"; 100];
も知らなかった。
iter_mut
は &mut
が帰ってくるから、値にアクセスするにはdereferenceをする必要がある。
めっちゃ便利なのに、こういうの忘れる
Enumの要素を舐める時、筋肉プレーをする必要があるのか
let fruit_kinds = vec![
Fruit::Apple,
Fruit::Banana,
Fruit::Mango,
Fruit::Lychee,
Fruit::Pineapple,
];
こんなにcloneして合っているのだろうか...?
let team_1_name = v[0].to_string();
let team_1_score: u8 = v[2].parse().unwrap();
let team_2_name = v[1].to_string();
let team_2_score: u8 = v[3].parse().unwrap();
// TODO: Populate the scores table with details extracted from the
// current line. Keep in mind that goals scored by team_1
// will be number of goals conceded from team_2, and similarly
// goals scored by team_2 will be the number of goals conceded by
// team_1.
let team_1_value = scores.entry(team_1_name.clone()).or_insert(Team {name: team_1_name.clone(), goals_scored: 0, goals_conceded: 0});
*team_1_value = Team{
name: team_1_name,
goals_scored: team_1_value.goals_scored + team_1_score,
goals_conceded: team_1_value.goals_conceded + team_2_score,
};
消費イテレータが強力すぎてびっくりしている。
型推論で全部解決してくれるのか
// Complete the function and return a value of the correct type so the test passes.
// Desired output: Ok([1, 11, 1426, 3])
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
let numbers = vec![27, 297, 38502, 81];
numbers.into_iter().map(|n| divide(n, 27)).collect()
}
// Complete the function and return a value of the correct type so the test passes.
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
let numbers = vec![27, 297, 38502, 81];
numbers.into_iter().map(|n| divide(n, 27)).collect()
}
返り値の型を書き換えただけで、全部 collect
がよしなにエラーハンドリングしてくれた...
fold
エレガントだけど、可読性的には不安
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
// collection is a slice of hashmaps.
// collection = [{ "variables1": Complete, "from_str": None, ... },
// { "variables2": Complete, ... }, ... ]
collection.iter().fold(0, |acc, map| acc + map.iter().filter(|x| x.1 == &value).count())
}
標準ライブラリのchannelが Multiple Producers Single Consumer と知った。
broadcastingみたいなことをしたければ、channelを量産する必要があるという理解。
こんな感じ?
いくつかライブラリはあるっぽいから、そういうのを使う?
tokioってめっちゃ聞く気がするからちゃんと勉強した方が良さそうな匂いを感じている。
clippy3.rs がいまいちわかっていない...
let my_empty_vec = vec![1, 2, 3, 4, 5].clear();
println!("This Vec is empty, see? {:?}", my_empty_vec);
に対して
error: this let-binding has unit value
--> clippy3.rs:16:5
|
16 | let my_empty_vec = vec![1, 2, 3, 4, 5].clear();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `vec![1, 2, 3, 4, 5].clear();`
|
= note: `-D clippy::let-unit-value` implied by `-D warnings`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
error: could not compile `clippy3` due to previous error
というエラーが出ていて、 letバインディングを単一の値?に使うな??と言われていたので、
println!("This Vec is empty, see? {:?}", vec![1, 2, 3, 4, 5].clear());
こうしたけど、あってるのか〜??
🎉 All exercises completed! 🎉
+----------------------------------------------------+
| You made it to the Fe-nish line! |
+-------------------------- ------------------------+
<ここに最高に面白い一言を入れる>
We hope you enjoyed learning about the various aspects of Rust!
If you noticed any issues, please don't hesitate to report them to our repo.
You can also contribute your own exercises to help the greater community!
Before reporting an issue or contributing, please read our guidelines:
https://github.com/rust-lang/rustlings/blob/main/CONTRIBUTING.md