Skip to content

Commit 2d1da2a

Browse files
authored
Merge pull request #1645 from mo8it/prober-types-structs3
Use u32 instead of i32
2 parents d79984d + 9c0581b commit 2d1da2a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

exercises/structs/structs3.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
struct Package {
1414
sender_country: String,
1515
recipient_country: String,
16-
weight_in_grams: i32,
16+
weight_in_grams: u32,
1717
}
1818

1919
impl Package {
20-
fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
21-
if weight_in_grams <= 0 {
22-
panic!("Can not ship a weightless package.")
20+
fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Package {
21+
if weight_in_grams < 10 {
22+
// This is not how you should handle errors in Rust,
23+
// but we will learn about error handling later.
24+
panic!("Can not ship a package with weight below 10 grams.")
2325
} else {
2426
Package {
2527
sender_country,
@@ -33,7 +35,7 @@ impl Package {
3335
// Something goes here...
3436
}
3537

36-
fn get_fees(&self, cents_per_gram: i32) -> ??? {
38+
fn get_fees(&self, cents_per_gram: u32) -> ??? {
3739
// Something goes here...
3840
}
3941
}
@@ -48,7 +50,7 @@ mod tests {
4850
let sender_country = String::from("Spain");
4951
let recipient_country = String::from("Austria");
5052

51-
Package::new(sender_country, recipient_country, -2210);
53+
Package::new(sender_country, recipient_country, 5);
5254
}
5355

5456
#[test]

0 commit comments

Comments
 (0)