structs and enums

This commit is contained in:
2026-01-16 08:20:49 +00:00
parent 2cd1c84285
commit 26f785a0ff
6 changed files with 23 additions and 19 deletions

View File

@@ -1,6 +1,3 @@
// Structs contain data, but can also have logic. In this exercise, we have
// defined the `Package` struct, and we want to test some logic attached to it.
#[derive(Debug)]
struct Package {
sender_country: String,
@@ -11,8 +8,6 @@ struct Package {
impl Package {
fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Self {
if weight_in_grams < 10 {
// This isn't how you should handle errors in Rust, but we will
// learn about error handling later.
panic!("Can't ship a package with weight below 10 grams");
}
@@ -23,15 +18,12 @@ impl Package {
}
}
// TODO: Add the correct return type to the function signature.
fn is_international(&self) {
// TODO: Read the tests that use this method to find out when a package
// is considered international.
fn is_international(&self) -> bool {
self.sender_country != self.recipient_country
}
// TODO: Add the correct return type to the function signature.
fn get_fees(&self, cents_per_gram: u32) {
// TODO: Calculate the package's fees.
fn get_fees(&self, cents_per_gram: u32) -> u32 {
cents_per_gram * self.weight_in_grams
}
}
@@ -68,7 +60,6 @@ mod tests {
let recipient_country = sender_country.clone();
let package = Package::new(sender_country, recipient_country, 1200);
assert!(!package.is_international());
}