Menu
CoddyTech

配送所での受付

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

4つのステップ(受け入れ、報告、発送、ポリシーに基づく料金設定)を通して、荷物の配送拠点を構築しましょう。ファイル構成と変更できないドライバーは固定されています。各ステップでは、前のステップの解答を拡張します。クレート全体がコンパイルできるよう、後のステップ用のプレースホルダーメソッドはすでに用意されています。現在の仕様で指定されている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));
}

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

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