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::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);
assert_eq!(state.color, (255, 0, 255));

View File

@ -1,8 +1,6 @@
// result1.rs
// Make this test pass! Execute `rustlings hint result1` for hints :)
// I AM NOT DONE
#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);
@ -14,7 +12,14 @@ enum CreationError {
impl PositiveNonzeroInteger {
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!
// Execute the command `rustlings hint if2` if you want a hint :)
// I AM NOT DONE
pub fn fizz_if_foo(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
} else if fizzish == "fuzz" {
"bar"
} else {
1
"baz"
}
}

View File

@ -1,23 +1,23 @@
// option1.rs
// Make me compile! Execute `rustlings hint option1` for hints
// I AM NOT DONE
// you can modify anything EXCEPT for this function's sig
fn print_number(maybe_number: Option<u16>) {
println!("printing: {}", maybe_number.unwrap());
}
fn main() {
print_number(13);
print_number(99);
print_number(Option::Some(13));
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 {
let number_to_add: u16 = {
((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
// Make me compile! Execute `rustlings hint option2` for hints
// I AM NOT DONE
fn main() {
let optional_value = Some(String::from("rustlings"));
// 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);
} else {
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>
// 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);
}
}

View File

@ -3,8 +3,6 @@
// 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,
@ -15,18 +13,20 @@ struct Package {
impl Package {
fn new(from: String, to: String, weight: f32) -> Package {
if weight <= 0.0 {
// Something goes here...
panic!("Package is lighter then air!")
} else {
return Package {from, to, weight};
}
}
fn is_international(&self) -> ??? {
fn is_international(&self) -> bool {
// 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...
self.weight * cost_per_kg
}
}
@ -58,7 +58,7 @@ mod tests {
let country_from = 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);

View File

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