primitive + struct faites

This commit is contained in:
Simon Halimi 2021-04-13 15:47:57 +02:00
parent 0b66b2945a
commit 9cd3c76ab8
9 changed files with 37 additions and 43 deletions

View file

@ -2,17 +2,15 @@
// Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :)
// I AM NOT DONE
fn main() {
// Booleans (`bool`)
let is_morning = true;
let is_morning: bool = true;
if is_morning {
println!("Good morning!");
}
let // Finish the rest of this line like the example! Or make it be false!
let is_evening: bool = false;
if is_evening {
println!("Good evening!");
}

View file

@ -1,9 +1,6 @@
// primitive_types2.rs
// Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :)
// I AM NOT DONE
fn main() {
// Characters (`char`)
@ -16,9 +13,8 @@ fn main() {
println!("Neither alphabetic nor numeric!");
}
let // Finish this line like the example! What's your favorite character?
// Try a letter, try a number, try a special character, try a character
// from a different language than your own, try an emoji!
let your_character = 'D';
if your_character.is_alphabetic() {
println!("Alphabetical!");
} else if your_character.is_numeric() {

View file

@ -2,10 +2,8 @@
// Create an array with at least 100 elements in it where the ??? is.
// Execute `rustlings hint primitive_types3` for hints!
// I AM NOT DONE
fn main() {
let a = ???
let a = String::from("Hello, world!");
if a.len() >= 100 {
println!("Wow, that's a big array!");

View file

@ -2,13 +2,11 @@
// Get a slice out of Array a where the ??? is so that the test passes.
// Execute `rustlings hint primitive_types4` for hints!!
// I AM NOT DONE
#[test]
fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5];
let nice_slice = ???
let nice_slice = &a[1..4];
assert_eq!([2, 3, 4], nice_slice)
}

View file

@ -2,11 +2,9 @@
// Destructure the `cat` tuple so that the println will work.
// Execute `rustlings hint primitive_types5` for hints!
// I AM NOT DONE
fn main() {
let cat = ("Furry McFurson", 3.5);
let /* your pattern here */ = cat;
let (name, age) = cat;
println!("{} is {} years old.", name, age);
}

View file

@ -3,13 +3,11 @@
// You can put the expression for the second element where ??? is so that the test passes.
// Execute `rustlings hint primitive_types6` for hints!
// I AM NOT DONE
#[test]
fn indexing_tuple() {
let numbers = (1, 2, 3);
// Replace below ??? with the tuple indexing syntax.
let second = ???;
let second = numbers.1;
assert_eq!(2, second,
"This is not the 2nd number in the tuple!")

View file

@ -1,13 +1,12 @@
// structs1.rs
// Address all the TODOs to make the tests pass!
// I AM NOT DONE
struct ColorClassicStruct {
// TODO: Something goes here
name: String,
hex: String,
}
struct ColorTupleStruct(/* TODO: Something goes here */);
struct ColorTupleStruct(String, String);
#[derive(Debug)]
struct UnitStruct;
@ -19,7 +18,10 @@ mod tests {
#[test]
fn classic_c_structs() {
// TODO: Instantiate a classic c struct!
// let green =
let green = ColorClassicStruct {
name: String::from("green"),
hex: String::from("#00FF00"),
};
assert_eq!(green.name, "green");
assert_eq!(green.hex, "#00FF00");
@ -28,7 +30,7 @@ mod tests {
#[test]
fn tuple_structs() {
// TODO: Instantiate a tuple struct!
// let green =
let green = ColorTupleStruct(String::from("green"), String::from("#00FF00"));
assert_eq!(green.0, "green");
assert_eq!(green.1, "#00FF00");
@ -37,7 +39,7 @@ mod tests {
#[test]
fn unit_structs() {
// TODO: Instantiate a unit struct!
// let unit_struct =
let unit_struct = UnitStruct;
let message = format!("{:?}s are fun!", unit_struct);
assert_eq!(message, "UnitStructs are fun!");

View file

@ -1,8 +1,5 @@
// structs2.rs
// Address all the TODOs to make the tests pass!
// I AM NOT DONE
#[derive(Debug)]
struct Order {
name: String,
@ -33,8 +30,15 @@ mod tests {
#[test]
fn your_order() {
let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above!
// let your_order =
let your_order = Order {
name: String::from("Hacker in Rust"),
year: order_template.year,
made_by_phone: order_template.made_by_phone,
made_by_mobile: order_template.made_by_mobile,
made_by_email: order_template.made_by_email,
item_number: order_template.item_number,
count: 1,
};
assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);

View file

@ -4,9 +4,7 @@
// Make the code compile and the tests pass!
// If you have issues execute `rustlings hint structs3`
// I AM NOT DONE
#[derive(Debug)]
#[derive(Debug, Default)]
struct Package {
sender_country: String,
recipient_country: String,
@ -16,7 +14,7 @@ struct Package {
impl Package {
fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
if weight_in_grams <= 0 {
// Something goes here...
panic!();
} else {
return Package {
sender_country,
@ -26,12 +24,16 @@ impl Package {
}
}
fn is_international(&self) -> ??? {
// Something goes here...
fn is_international(&self) -> bool {
if self.sender_country == self.recipient_country {
false
} else {
true
}
}
fn get_fees(&self, cents_per_gram: i32) -> ??? {
// Something goes here...
fn get_fees(&self, cents_per_gram: i32) -> i32 {
return cents_per_gram * self.weight_in_grams;
}
}
@ -73,7 +75,7 @@ mod tests {
let sender_country = String::from("Spain");
let recipient_country = String::from("Spain");
let cents_per_gram = ???;
let cents_per_gram = 3;
let package = Package::new(sender_country, recipient_country, 1500);