Skip to content

Commit

Permalink
feat: Added exercise structs3.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
DiD92 committed Apr 27, 2020
1 parent 32721bb commit b66e2e0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
67 changes: 67 additions & 0 deletions exercises/structs/structs3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// structs3.rs
// Structs contain more than simply some data, they can also have logic, in this
// exercise we have defined the Package struct and we want to test some logic attached to it,
// make the code compile and the tests pass! If you have issues execute `rustlings hint structs3`

// I AM NOT DONE

#[derive(Debug)]
struct Package {
from: String,
to: String,
weight: f32
}

impl Package {
fn new(from: String, to: String, weight: f32) -> Package {
if weight <= 0.0 {
// Something goes here...
} else {
return Package {from, to, weight};
}
}

fn is_international(&self) -> ??? {
// Something goes here...
}

fn get_fees(&self, cost_per_kg: f32) -> ??? {
// Something goes here...
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic]
fn fail_creating_weightless_package() {
let country_from = String::from("Spain");
let country_to = String::from("Austria");

Package::new(country_from, country_to, -2.21);
}

#[test]
fn create_international_package() {
let country_from = String::from("Spain");
let country_to = String::from("Russia");

let package = Package::new(country_from, country_to, 1.2);

assert!(package.is_international());
}

#[test]
fn calculate_transport_fees() {
let country_from = String::from("Spain");
let country_to = String::from("Spain");

let country_fee = ???;

let package = Package::new(country_from, country_to, 22.0);

assert_eq!(package.get_fees(country_fee), 176.0);
}
}
13 changes: 13 additions & 0 deletions info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ Creating instances of structs is easy, all you need to do is assign some values
There is however some shortcuts that can be taken when instantiating structs.
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"""

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

# STRINGS

[[exercises]]
Expand Down

0 comments on commit b66e2e0

Please sign in to comment.