Skip to content

Commit 7c4b1f9

Browse files
author
fmoko
authored
Merge pull request #372 from DiD92/exercise_structs3
2 parents 3ceabe9 + b66e2e0 commit 7c4b1f9

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

exercises/structs/structs3.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// structs3.rs
2+
// Structs contain more than simply some data, they can also have logic, in this
3+
// exercise we have defined the Package struct and we want to test some logic attached to it,
4+
// make the code compile and the tests pass! If you have issues execute `rustlings hint structs3`
5+
6+
// I AM NOT DONE
7+
8+
#[derive(Debug)]
9+
struct Package {
10+
from: String,
11+
to: String,
12+
weight: f32
13+
}
14+
15+
impl Package {
16+
fn new(from: String, to: String, weight: f32) -> Package {
17+
if weight <= 0.0 {
18+
// Something goes here...
19+
} else {
20+
return Package {from, to, weight};
21+
}
22+
}
23+
24+
fn is_international(&self) -> ??? {
25+
// Something goes here...
26+
}
27+
28+
fn get_fees(&self, cost_per_kg: f32) -> ??? {
29+
// Something goes here...
30+
}
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
use super::*;
36+
37+
#[test]
38+
#[should_panic]
39+
fn fail_creating_weightless_package() {
40+
let country_from = String::from("Spain");
41+
let country_to = String::from("Austria");
42+
43+
Package::new(country_from, country_to, -2.21);
44+
}
45+
46+
#[test]
47+
fn create_international_package() {
48+
let country_from = String::from("Spain");
49+
let country_to = String::from("Russia");
50+
51+
let package = Package::new(country_from, country_to, 1.2);
52+
53+
assert!(package.is_international());
54+
}
55+
56+
#[test]
57+
fn calculate_transport_fees() {
58+
let country_from = String::from("Spain");
59+
let country_to = String::from("Spain");
60+
61+
let country_fee = ???;
62+
63+
let package = Package::new(country_from, country_to, 22.0);
64+
65+
assert_eq!(package.get_fees(country_fee), 176.0);
66+
}
67+
}

info.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,19 @@ Creating instances of structs is easy, all you need to do is assign some values
241241
There is however some shortcuts that can be taken when instantiating structs.
242242
Have a look in The Book, to find out more: https://doc.rust-lang.org/stable/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax"""
243243

244+
[[exercises]]
245+
name = "structs3"
246+
path = "exercises/structs/structs3.rs"
247+
mode = "test"
248+
hint = """
249+
The new method needs to panic if the weight is physically impossible :), how do we do that in Rust?
250+
251+
For is_international: What makes a package international? Seems related to the places it goes through right?
252+
253+
For calculate_transport_fees: Bigger is more expensive usually, we don't have size, but something may fit the bill here :)
254+
255+
Have a look in The Book, to find out more about method implementations: https://doc.rust-lang.org/book/ch05-03-method-syntax.html"""
256+
244257
# STRINGS
245258

246259
[[exercises]]

0 commit comments

Comments
 (0)