Rust と AtCoder を勉強するうちに「これよく使うな」と思った処理をまとめていきます!
随時更新していきます!
HashMap
から key: K
で指定した値を Entry
として取り出します。
Entry
が空の場合は default: V
を挿入します。その後 Entry
の値への参照を返します。
Vec
を作るstd::T::MIN
, std::T::MAX
のようにします。
符号なし整数の最小値は当然 なので、引き算の結果負の数になるとエラーが発生します。
Error
this arithmetic operation will overflow
attempt to compute `1_u8 - 5_u8`, which would overflow
this arithmetic operation will overflow
saturating_sub()
を使用すると、計算結果は型の最小値・最大値の中に抑えこまれます。つまり、usize
同士の引き算で負になる場合、最小値の になるということです。
Vec<f64>
, Vec<f32>
をソートするVec<f64>
や Vec<f32>
をソートしようとすると、以下のようにエラーが発生します。
Error
the trait bound `f64: Ord` is not satisfied
a.sort();
^^^^ the trait `Ord` is not implemented for `f64`
the trait bound `f64: Ord` is not satisfied
こうしましょう。