Định giá tại kho
Bài học 46 trên 48 trong khóa học Rust dành cho lập trình viên R của Coddy.
Thử thách
Trung bìnhGiữ nguyên mọi hành vi trước đó. Triển khai các trường base: u32 và rate: u32 của Linear, trong đó Fee::fee(&self, weight: u32) -> u32 bằng base cộng rate nhân với weight. Triển khai Depot::quote<P: Fee + ?Sized>(&self, policy: &P) -> u32 bằng cách yêu cầu policy được truyền vào tính phí cho từng parcel rồi tính tổng; nếu không có parcel thì trả về 0. Đừng giả định người gọi chỉ dùng Linear, các policy tùy chỉnh và đối tượng trait cũng sẽ được kiểm tra. Giai đoạn 4 dùng base bằng 2 và rate bằng 3, rồi xuất tổng phí đã tính. Giai đoạn này không xóa target. Các giai đoạn 1, 2 và 3 vẫn được kiểm tra.
Triển khai các API cần thiết được xuất ra và trả về kết quả từ exercise::solve(input: &str) -> String mà không có ký tự xuống dòng ở cuối. Đừng in ra trong code có thể chỉnh sửa. Trình điều khiển bị khóa kiểm tra riêng các API cần thiết, đọc toàn bộ đầu vào chuẩn và in văn bản được trả về với một ký tự xuống dòng ở cuối.
Code của bạn nằm trong các tệp exercise.rs, parcel.rs, depot.rs, policy.rs, linear.rs. main.rs chứa code đầu vào và đầu ra được cung cấp, bạn không thể chỉnh sửa tệp này.
Tự làm thử
#![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();
// BẮT ĐẦU KIỂM TRA API CÔNG KHAI
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 = □
assert_eq!(d.quote(policy), 53); assert_eq!(d.quote(&Square), 53);
assert_eq!(depot::Depot::new().quote(&Square), 0);
}
// KẾT THÚC KIỂM TRA API CÔNG KHAI
println!("{}", exercise::solve(&input));
}
Tất cả bài học trong Rust dành cho lập trình viên R
1Những chương trình Rust đầu tiên
Chạy các module RustĐầu ra và chú thíchModule và khả năng hiển thịBiến và kiểu dữ liệuSố và phép ép kiểuÔn tập: Hóa đơn lưu trữ4Quyền sở hữu và mượn
Quyền sở hữu và chuyển quyềnMượn dùng chungMượn có thể thay đổiVòng đời và chế độ xemÔn tập về mượn6Vắng mặt và thất bại
Tùy chọn và đối sánhKết quả và lan truyềnPhân tích cú pháp và xác thựcQuy ước về lỗiÔn tập về thất bại9Dự án: Kho bưu kiện
Tiếp nhận hàng tại khoBáo cáo trọng lượng tại khoTự luyện tập: Trình biên dịch Rust online