Menu
CoddyTech

荷物の受付

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

4つの段階を通して、荷物の集配所を作りましょう。Goで設計するなら、構造体のスライスと、インターフェース型の料金ルールを使うかもしれません。ここでは、所有する荷物の値を集配所内に保持し、Rustのメソッドで操作を公開します。

この最初の段階では、荷物の受け入れを確立します。後の段階では、先行するAPIを維持しながら、重量の報告、発送、料金設定を追加します。各続きは、直前の完成した解答を基に始まります。同じ固定されたドライバーが入力から段階を読み取り、その段階で利用できるAPIを確認します。

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 では個数を出力します。用意されている演習コードは 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));
}

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

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