Merge pull request from 0xchasingfire/excercises-d3

err/generics/option/quiz/test/traits
This commit is contained in:
0xChasingFire 2021-05-24 12:27:50 +08:00 committed by GitHub
commit 78bce549a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 40 additions and 47 deletions

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,13 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
Ok(PositiveNonzeroInteger(value as u64))
if value > 0{
Ok(PositiveNonzeroInteger(value as u64))
} else if value < 0 {
Err(CreationError::Negative)
} else {
Err(CreationError::Zero)
}
}
}

View file

@ -3,9 +3,7 @@
// Execute `rustlings hint generics1` for hints!
// I AM NOT DONE
fn main() {
let mut shopping_list: Vec<?> = Vec::new();
let mut shopping_list: Vec<&str> = Vec::new();
shopping_list.push("milk");
}

View file

@ -3,14 +3,12 @@
// Execute `rustlings hint generics2` for hints!
// I AM NOT DONE
struct Wrapper {
value: u32,
struct Wrapper<T> {
value: T,
}
impl Wrapper {
pub fn new(value: u32) -> Self {
impl<T> Wrapper<T> {
pub fn new(value: T) -> Self {
Wrapper { value }
}
}

View file

@ -10,15 +10,13 @@
// Execute 'rustlings hint generics3' for hints!
// I AM NOT DONE
pub struct ReportCard {
pub grade: f32,
pub struct ReportCard<T: std::fmt::Display> {
pub grade: T,
pub student_name: String,
pub student_age: u8,
}
impl ReportCard {
impl<T: std::fmt::Display> ReportCard<T> {
pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}",
&self.student_name, &self.student_age, &self.grade)
@ -46,7 +44,7 @@ mod tests {
fn generate_alphabetic_report_card() {
// TODO: Make sure to change the grade here after you finish the exercise.
let report_card = ReportCard {
grade: 2.1,
grade: "A+",
student_name: "Gary Plotter".to_string(),
student_age: 11,
};

View file

@ -1,23 +1,21 @@
// 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(Some(13));
print_number(Some(99));
let mut numbers: [Option<u16>; 5];
let mut numbers: [Option<u16>; 5] = [Some(0); 5];
for iter in 0..5 {
let number_to_add: u16 = {
((iter * 1235) + 2) / (4 * 16)
};
numbers[iter as usize] = number_to_add;
numbers[iter as usize] = 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_word = Some(String::from("rustlings"));
// TODO: Make this an if let statement whose value is "Some" type
word = optional_word {
if let Some(word) = optional_word {
println!("The word is: {}", word);
} else {
println!("The optional word 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
integer = optional_integers_vec.pop() {
while let Some(Some(integer)) = optional_integers_vec.pop() {
println!("current value: {}", integer);
}
}

View file

@ -1,8 +1,6 @@
// option3.rs
// Make me compile! Execute `rustlings hint option3` for hints
// I AM NOT DONE
struct Point {
x: i32,
y: i32,
@ -12,7 +10,7 @@ fn main() {
let y: Option<Point> = Some(Point { x: 100, y: 200 });
match y {
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
_ => println!("no match"),
}
y; // Fix without deleting this line.

View file

@ -7,8 +7,6 @@
// we expect to get when we call `times_two` with a negative number.
// No hints, you can do this :)
// I AM NOT DONE
pub fn times_two(num: i32) -> i32 {
num * 2
}
@ -19,12 +17,13 @@ mod tests {
#[test]
fn returns_twice_of_positive_numbers() {
assert_eq!(times_two(4), ???);
assert_eq!(times_two(4), 8);
}
#[test]
fn returns_twice_of_negative_numbers() {
// TODO replace unimplemented!() with an assert for `times_two(-4)`
unimplemented!()
// unimplemented!()
assert_eq!(times_two(-3), -6)
}
}

View file

@ -6,12 +6,11 @@
// This test has a problem with it -- make the test compile! Make the test
// pass! Make the test fail! Execute `rustlings hint tests1` for hints :)
// I AM NOT DONE
#[cfg(test)]
mod tests {
#[test]
fn you_can_assert() {
assert!();
assert!(true);
}
}

View file

@ -2,12 +2,11 @@
// This test has a problem with it -- make the test compile! Make the test
// pass! Make the test fail! Execute `rustlings hint tests2` for hints :)
// I AM NOT DONE
#[cfg(test)]
mod tests {
#[test]
fn you_can_assert_eq() {
assert_eq!();
assert_eq!(1, 1);
}
}

View file

@ -4,8 +4,6 @@
// we expect to get when we call `is_even(5)`.
// Execute `rustlings hint tests3` for hints :)
// I AM NOT DONE
pub fn is_even(num: i32) -> bool {
num % 2 == 0
}
@ -16,11 +14,11 @@ mod tests {
#[test]
fn is_true_when_even() {
assert!();
assert!(is_even(4));
}
#[test]
fn is_false_when_odd() {
assert!();
assert!(!is_even(3));
}
}

View file

@ -8,14 +8,16 @@
// which appends "Bar" to any object
// implementing this trait.
// I AM NOT DONE
trait AppendBar {
fn append_bar(self) -> Self;
}
impl AppendBar for String {
//Add your code here
fn append_bar(self) -> String{
// format!("{}{}", String::from(self), "Bar")
self + "Bar"
}
}
fn main() {

View file

@ -10,13 +10,17 @@
// No boiler plate code this time,
// you can do this!
// I AM NOT DONE
trait AppendBar {
fn append_bar(self) -> Self;
}
//TODO: Add your code here
impl AppendBar for Vec<String>{
fn append_bar(mut self) -> Vec<String> {
self.push("Bar".to_string());
self
}
}
#[cfg(test)]
mod tests {