Menu
CoddyTech

集散站收件

Coddy 面向 C 开发者的 Rust 课程的第 43 课,共 48 课。

分四步搭建一个包裹站点:收件、报告、派送和基于策略的定价。文件布局和锁定的驱动程序保持不变。每一步都在前一步的答案基础上扩展。后续步骤的占位方法已经存在,因此整个 crate 可以编译;只需完成当前契约中指定的 API。

challenge icon

挑战

中等

构建一个包裹仓库。输入以 stage target 开始,后面跟着零行或多行 code weight。编码是 ASCII 单词,重量是 0 到 100 的整数,最多 20 行。所有算术运算都不会超出 u32 的范围。忽略无效包裹和重复编码,保留第一个有效包裹。Parcel::new(code: &str, weight: u32) -> Result<Parcel, &'static str> 会拒绝空编码或重量为零的包裹,并返回 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 阶段,输出包裹数量。提供的练习代码会分派全部四个阶段的命令;后续阶段的方法起初会返回占位值。只实现当前步骤要求的 API。后续步骤中要保留之前的所有行为。

实现所需的导出 API,并从 exercise::solve(input: &str) -> String 返回结果,末尾不要添加换行符。不要在可编辑代码中打印内容。锁定的驱动程序会独立检查所需的 API,读取全部标准输入,并在打印返回的文本时添加一个末尾换行符。

你的代码要写在 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 在线编译器