Menu
CoddyTech

デポの荷受

Coddyの「Dart開発者のためのRust」コースのレッスン 43/48。

4つの接続されたステージにわたって小包デポを構築します。Dartのクラスインスタンスのリストはオブジェクト参照を共有できますが、このRustの設計では各小包を所有し、観察のためにそれを借用します。まずは検証済みの受付と重複検出から始め、次に重量レポート、所有権の移動による削除、そして注入された価格設定トレイトを追加します。各継続ステップは、前の完全なプログラムから始まります。以前の動作と提供されたドライバーを維持してください。後続のチェックでも引き続き以前のステージがテストされます。

challenge icon

チャレンジ

中級

小包デポ(保管庫)を構築します。入力はstage targetで始まり、その後に0行以上のcode weight行が続きます。コードはASCII単語で、重量は0から100までの整数で、最大20行です。すべての算術演算はu32に収まります。無効な小包や重複するコードは無視し、最初の有効な小包を保持します。Parcel::new(code: &str, weight: u32) -> Result<Parcel, &'static str>は、空のコードまたは重量0をErr("invalid")で拒否します。code(&self) -> &strおよびweight(&self) -> u32はそのデータを公開します。Depot::new() -> Depotは空で開始し、len(&self) -> usizeは小包の数を数えます。add(&mut self, parcel: Parcel) -> Result<(), &'static str>は所有権を受け取り、保存された小包を変更することなく、重複するコードをErr("duplicate")で拒否します。ステージ1では件数を出力してください。提供されているexerciseは4つすべてのステージコマンドを呼び出します。将来のメソッドは初期状態ではプレースホルダーを返します。現在のステップで要求されているAPIのみを実装してください。以降のステップでも以前の動作をすべて維持してください。

必要な公開APIを実装し、末尾の改行なしでexercise::solve(input: &str) -> Stringから結果を返してください。編集可能なコード内で出力を行わないでください。ロックされたドライバーは必要なAPIを独立してチェックし、すべての標準入力を読み込んで、返されたテキストの末尾に改行を1つ付けて出力します。

コードはexercise.rs、parcel.rs、depot.rs、policy.rs、linear.rsに記述します。main.rsには提供された入出力コードが含まれており、編集できません。

自分で試してみよう

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod exercise;
mod parcel;
mod depot;
mod policy;
mod linear;
use std::io::{self, Read};
fn main() {
    let mut input=String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    // パブリックAPIプローブの開始
let stage: u32 = input.split_whitespace().next().unwrap().parse().unwrap();
    use policy::Fee;
    assert_eq!(parcel::Parcel::new("", 8), Err("invalid"));
    assert_eq!(parcel::Parcel::new("bad", 0), Err("invalid"));
    let a = parcel::Parcel::new("held", 7).unwrap();
    assert_eq!(a.code(), "held"); assert_eq!(a.weight(), 7);
    let mut d = depot::Depot::new(); assert_eq!(d.len(), 0);
    assert!(d.add(a).is_ok());
    assert_eq!(d.add(parcel::Parcel::new("held", 99).unwrap()), Err("duplicate")); assert_eq!(d.len(), 1);
    d.add(parcel::Parcel::new("other", 7).unwrap()).unwrap();
    if stage >= 2 {
        assert_eq!(d.total_weight(), 14); assert_eq!(d.total_weight(), 14);
        assert_eq!(d.heaviest().unwrap().code(), "held");
        let empty = depot::Depot::new(); assert_eq!(empty.total_weight(), 0); assert!(empty.heaviest().is_none());
        let mut varied = depot::Depot::new();
        varied.add(parcel::Parcel::new("light", 3).unwrap()).unwrap();
        varied.add(parcel::Parcel::new("heavy", 19).unwrap()).unwrap();
        varied.add(parcel::Parcel::new("last", 8).unwrap()).unwrap();
        assert_eq!(varied.heaviest().unwrap().code(), "heavy"); assert_eq!(varied.total_weight(), 30);
    }
    if stage >= 3 {
        assert!(d.remove("absent").is_none()); assert_eq!(d.len(), 2); assert_eq!(d.total_weight(), 14);
        assert_eq!(d.remove("held").unwrap().weight(), 7); assert!(d.remove("held").is_none());
        assert_eq!(d.len(), 1); assert_eq!(d.heaviest().unwrap().code(), "other");
        d.add(parcel::Parcel::new("held", 2).unwrap()).unwrap(); assert_eq!(d.total_weight(), 9);
    }
    if stage >= 4 {
        struct Square;
        impl policy::Fee for Square { fn fee(&self, w: u32) -> u32 { w * w } }
        assert_eq!(linear::Linear { base: 5, rate: 2 }.fee(4), 13);
        let policy: &dyn policy::Fee = &Square;
        assert_eq!(d.quote(policy), 53); assert_eq!(d.quote(&Square), 53);
        assert_eq!(depot::Depot::new().quote(&Square), 0);
    }

    // パブリックAPIプローブの終了
    println!("{}", exercise::solve(&input));
}

Dart開発者のためのRustのすべてのレッスン

自分で練習してみよう: Rustオンラインコンパイラ