|
| 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 | +} |
0 commit comments