Menu
CoddyTech

Depot Intake

Lesson 43 of 48 in Coddy's Rust for Dart Developers course.

Build a parcel depot across four connected stages. Dart lists of class instances can share object references; this Rust design owns each parcel and borrows it for observation. Start with validated intake and duplicate detection, then add weight reporting, removal by ownership transfer, and an injected pricing trait. Each continuation begins with the previous complete program. Preserve earlier behavior and the supplied driver; the later checks still exercise earlier stages.

challenge icon

Challenge

Medium

Build a parcel depot. Input begins with stage target, followed by zero or more code weight lines. Codes are ASCII words, weights are integers from 0 to 100, at most 20 rows. All arithmetic fits u32. Ignore invalid parcels and duplicate codes, keeping the first valid parcel. Parcel::new(code: &str, weight: u32) -> Result<Parcel, &'static str> rejects empty codes or zero weight with Err("invalid"); code(&self) -> &str and weight(&self) -> u32 expose its data. Depot::new() -> Depot starts empty; len(&self) -> usize counts parcels. add(&mut self, parcel: Parcel) -> Result<(), &'static str> takes ownership and rejects duplicate codes with Err("duplicate") without altering stored parcels. In stage 1 output the count. The supplied exercise dispatches all four stage commands; future methods initially return placeholders. Only implement the APIs required by the current step. Keep all earlier behavior in later steps.

Implement the required exported APIs and return the result from exercise::solve(input: &str) -> String without a final newline. Do not print in editable code. The locked driver checks required APIs independently, reads all stdin, and prints the returned text with one final newline.

Your code goes in exercise.rs, parcel.rs, depot.rs, policy.rs, linear.rs. main.rs holds the supplied input/output code and cannot be edited.

Try it yourself

#![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();
    // BEGIN PUBLIC API PROBES
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);
    }

    // END PUBLIC API PROBES
    println!("{}", exercise::solve(&input));
}

All lessons in Rust for Dart Developers

Practice on your own: Online Rust compiler