summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
authorfmoko <mokou@posteo.de>2020-05-03 19:44:26 +0200
committerGitHub <noreply@github.com>2020-05-03 19:44:26 +0200
commit7c4b1f910c2214feca2046817048372afc520f82 (patch)
tree0098c6516feea01fc7dc41bfeaeab49c0676f0d8 /exercises
parent3ceabe91f81944608b96e175ec7c3327235f691d (diff)
parentb66e2e09622243e086a0f1258dd27e1a2d61c891 (diff)
Merge pull request #372 from DiD92/exercise_structs3
Diffstat (limited to 'exercises')
-rw-r--r--exercises/structs/structs3.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/exercises/structs/structs3.rs b/exercises/structs/structs3.rs
new file mode 100644
index 0000000..77cc154
--- /dev/null
+++ b/exercises/structs/structs3.rs
@@ -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);
+ }
+}