Solve new exercises

This commit is contained in:
Enrico Bozzolini 2020-05-22 23:18:02 +02:00
parent cd369c68ae
commit d3a2dd6b64
7 changed files with 27 additions and 26 deletions

View File

@ -64,7 +64,7 @@ mod tests {
}; };
state.process(Message::ChangeColor(255, 0, 255)); state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Echo(String::from("hello world"))); state.process(Message::Echo(String::from("hello world")));
state.process(Message::Move(Point{ x: 10, y: 15 })); state.process(Message::Move{ x: 10, y: 15 });
state.process(Message::Quit); state.process(Message::Quit);
assert_eq!(state.color, (255, 0, 255)); assert_eq!(state.color, (255, 0, 255));

View File

@ -1,8 +1,6 @@
// result1.rs // result1.rs
// Make this test pass! Execute `rustlings hint result1` for hints :) // Make this test pass! Execute `rustlings hint result1` for hints :)
// I AM NOT DONE
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64); struct PositiveNonzeroInteger(u64);
@ -14,7 +12,14 @@ enum CreationError {
impl PositiveNonzeroInteger { impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> { fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
Ok(PositiveNonzeroInteger(value as u64)) if value > 1 {
Ok(PositiveNonzeroInteger(value as u64))
} else if value == 0 {
Err(CreationError::Zero)
} else {
Err(CreationError::Negative)
}
} }
} }

View File

@ -4,13 +4,13 @@
// Step 2: Get the bar_for_fuzz and default_to_baz tests passing! // Step 2: Get the bar_for_fuzz and default_to_baz tests passing!
// Execute the command `rustlings hint if2` if you want a hint :) // Execute the command `rustlings hint if2` if you want a hint :)
// I AM NOT DONE
pub fn fizz_if_foo(fizzish: &str) -> &str { pub fn fizz_if_foo(fizzish: &str) -> &str {
if fizzish == "fizz" { if fizzish == "fizz" {
"foo" "foo"
} else if fizzish == "fuzz" {
"bar"
} else { } else {
1 "baz"
} }
} }

View File

@ -1,23 +1,23 @@
// option1.rs // option1.rs
// Make me compile! Execute `rustlings hint option1` for hints // Make me compile! Execute `rustlings hint option1` for hints
// I AM NOT DONE
// you can modify anything EXCEPT for this function's sig // you can modify anything EXCEPT for this function's sig
fn print_number(maybe_number: Option<u16>) { fn print_number(maybe_number: Option<u16>) {
println!("printing: {}", maybe_number.unwrap()); println!("printing: {}", maybe_number.unwrap());
} }
fn main() { fn main() {
print_number(13); print_number(Option::Some(13));
print_number(99); print_number(Option::Some(99));
let mut numbers: [Option<u16>; 5]; let mut numbers: [Option<u16>; 5] = [
Option::Some(0), Option::Some(0), Option::Some(0), Option::Some(0), Option::Some(0)
];
for iter in 0..5 { for iter in 0..5 {
let number_to_add: u16 = { let number_to_add: u16 = {
((iter * 5) + 2) / (4 * 16) ((iter * 5) + 2) / (4 * 16)
}; };
numbers[iter as usize] = number_to_add; numbers[iter as usize] = Option::Some(number_to_add);
} }
} }

View File

@ -1,12 +1,10 @@
// option2.rs // option2.rs
// Make me compile! Execute `rustlings hint option2` for hints // Make me compile! Execute `rustlings hint option2` for hints
// I AM NOT DONE
fn main() { fn main() {
let optional_value = Some(String::from("rustlings")); let optional_value = Some(String::from("rustlings"));
// TODO: Make this an if let statement whose value is "Some" type // TODO: Make this an if let statement whose value is "Some" type
value = optional_value { if let Some(value) = optional_value {
println!("the value of optional value is: {}", value); println!("the value of optional value is: {}", value);
} else { } else {
println!("The optional value doesn't contain anything!"); println!("The optional value doesn't contain anything!");
@ -19,7 +17,7 @@ fn main() {
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T> // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
// You can stack `Option<T>`'s into while let and if let // You can stack `Option<T>`'s into while let and if let
value = optional_values_vec.pop() { while let Some(Some(value)) = optional_values_vec.pop() {
println!("current value: {}", value); println!("current value: {}", value);
} }
} }

View File

@ -3,8 +3,6 @@
// exercise we have defined the Package struct and we want to test some logic attached to it, // 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` // make the code compile and the tests pass! If you have issues execute `rustlings hint structs3`
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Package { struct Package {
from: String, from: String,
@ -15,18 +13,20 @@ struct Package {
impl Package { impl Package {
fn new(from: String, to: String, weight: f32) -> Package { fn new(from: String, to: String, weight: f32) -> Package {
if weight <= 0.0 { if weight <= 0.0 {
// Something goes here... panic!("Package is lighter then air!")
} else { } else {
return Package {from, to, weight}; return Package {from, to, weight};
} }
} }
fn is_international(&self) -> ??? { fn is_international(&self) -> bool {
// Something goes here... // Something goes here...
self.from != self.to
} }
fn get_fees(&self, cost_per_kg: f32) -> ??? { fn get_fees(&self, cost_per_kg: f32) -> f32 {
// Something goes here... // Something goes here...
self.weight * cost_per_kg
} }
} }
@ -58,7 +58,7 @@ mod tests {
let country_from = String::from("Spain"); let country_from = String::from("Spain");
let country_to = String::from("Spain"); let country_to = String::from("Spain");
let country_fee = ???; let country_fee = 8.0;
let package = Package::new(country_from, country_to, 22.0); let package = Package::new(country_from, country_to, 22.0);

View File

@ -1,9 +1,7 @@
// variables5.rs // variables5.rs
// Make me compile! Execute the command `rustlings hint variables6` if you want a hint :) // Make me compile! Execute the command `rustlings hint variables6` if you want a hint :)
// I AM NOT DONE const NUMBER: u8 = 3;
const NUMBER = 3;
fn main() { fn main() {
println!("Number {}", NUMBER); println!("Number {}", NUMBER);
} }