update again

This commit is contained in:
tab 2020-05-17 00:59:07 +08:00
parent eea0a2957e
commit 0babb9faf7
7 changed files with 38 additions and 47 deletions

View file

@ -1,10 +1,8 @@
// This shopping list program isn't compiling! // This shopping list program isn't compiling!
// Use your knowledge of generics to fix it. // Use your knowledge of generics to fix it.
// I AM NOT DONE
fn main() { fn main() {
let mut shopping_list: Vec<?> = Vec::new(); let mut shopping_list: Vec<&str> = Vec::new();
shopping_list.push("milk"); shopping_list.push("milk");
} }

View file

@ -1,13 +1,12 @@
// This powerful wrapper provides the ability to store a positive integer value. // This powerful wrapper provides the ability to store a positive integer value.
// Rewrite it using generics so that it supports wrapping ANY type. // Rewrite it using generics so that it supports wrapping ANY type.
// I AM NOT DONE struct Wrapper<T> {
struct Wrapper<u32> { value: T
value: u32
} }
impl<u32> Wrapper<u32> { impl<T> Wrapper<T> {
pub fn new(value: u32) -> Self { pub fn new(value: T) -> Self {
Wrapper { value } Wrapper { value }
} }
} }
@ -18,13 +17,11 @@ mod tests {
#[test] #[test]
fn store_u32_in_wrapper() { fn store_u32_in_wrapper() {
assert_eq!(Wrapper::new(42).value, 42); assert_eq!(Wrapper::new(42).value, 42);
} }
#[test] #[test]
fn store_str_in_wrapper() { fn store_str_in_wrapper() {
// TODO: Delete this assert and uncomment the one below once you have finished the exercise. assert_eq!(Wrapper::new("Foo").value, "Foo");
assert!(false);
// assert_eq!(Wrapper::new("Foo").value, "Foo");
} }
} }

View file

@ -1,19 +1,19 @@
// An imaginary magical school has a new report card generation system written in rust! // An imaginary magical school has a new report card generation system written in rust!
// Currently the system only supports creating report cards where the student's grade // Currently the system only supports creating report cards where the student's grade
// is represented numerically (e.g. 1.0 -> 5.5). However, the school also issues alphabetical grades // is represented numerically (e.g. 1.0 -> 5.5). However, the school also issues alphabetical grades
// (A+ -> F-) and needs to be able to print both types of report card! // (A+ -> F-) and needs to be able to print both types of report card!
// Make the necessary code changes to support alphabetical report cards, thereby making the second // Make the necessary code changes to support alphabetical report cards, thereby making the second
// test pass. // test pass.
use std::fmt::Display;
// I AM NOT DONE pub struct ReportCard<T> {
pub struct ReportCard { pub grade: T,
pub grade: f32,
pub student_name: String, pub student_name: String,
pub student_age: u8, pub student_age: u8,
} }
impl ReportCard { impl<T:Display> ReportCard<T> {
pub fn print(&self) -> String { pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade) format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade)
} }
@ -26,8 +26,8 @@ mod tests {
#[test] #[test]
fn generate_numeric_report_card() { fn generate_numeric_report_card() {
let report_card = ReportCard { let report_card = ReportCard {
grade: 2.1, grade: 2.1,
student_name: "Tom Wriggle".to_string(), student_name: "Tom Wriggle".to_string(),
student_age: 12, student_age: 12,
}; };
assert_eq!(report_card.print(), "Tom Wriggle (12) - achieved a grade of 2.1"); assert_eq!(report_card.print(), "Tom Wriggle (12) - achieved a grade of 2.1");
@ -37,10 +37,10 @@ mod tests {
fn generate_alphabetic_report_card() { fn generate_alphabetic_report_card() {
// TODO: Make sure to change the grade here after you finish the exercise. // TODO: Make sure to change the grade here after you finish the exercise.
let report_card = ReportCard { let report_card = ReportCard {
grade: 2.1, grade: "A+",
student_name: "Gary Plotter".to_string(), student_name: "Gary Plotter".to_string(),
student_age: 11, student_age: 11,
}; };
assert_eq!(report_card.print(), "Gary Plotter (11) - achieved a grade of A+"); assert_eq!(report_card.print(), "Gary Plotter (11) - achieved a grade of A+");
} }
} }

View file

@ -7,8 +7,6 @@
// Execute `rustlings hint iterators3` to get some hints! // Execute `rustlings hint iterators3` to get some hints!
// Have fun :-) // Have fun :-)
// I AM NOT DONE
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum DivisionError { pub enum DivisionError {
NotDivisible(NotDivisibleError), NotDivisible(NotDivisibleError),
@ -70,12 +68,11 @@ mod tests {
} }
// Iterator exercises using your `divide` function // Iterator exercises using your `divide` function
/*
#[test] #[test]
fn result_with_list() { fn result_with_list() {
let numbers = vec![27, 297, 38502, 81]; let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27)); let division_results = numbers.into_iter().map(|n| divide(n, 27));
let x //... Fill in here! let x = division_results.collect::<Result<Vec<_>, _>>();
assert_eq!(format!("{:?}", x), "Ok([1, 11, 1426, 3])"); assert_eq!(format!("{:?}", x), "Ok([1, 11, 1426, 3])");
} }
@ -83,8 +80,7 @@ mod tests {
fn list_of_results() { fn list_of_results() {
let numbers = vec![27, 297, 38502, 81]; let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27)); let division_results = numbers.into_iter().map(|n| divide(n, 27));
let x //... Fill in here! let x = division_results.collect::<Vec<_>>();
assert_eq!(format!("{:?}", x), "[Ok(1), Ok(11), Ok(1426), Ok(3)]"); assert_eq!(format!("{:?}", x), "[Ok(1), Ok(11), Ok(1426), Ok(3)]");
} }
*/
} }

View file

@ -1,7 +1,5 @@
// iterators4.rs // iterators4.rs
// I AM NOT DONE
pub fn factorial(num: u64) -> u64 { pub fn factorial(num: u64) -> u64 {
// Complete this function to return the factorial of num // Complete this function to return the factorial of num
// Do not use: // Do not use:
@ -12,6 +10,7 @@ pub fn factorial(num: u64) -> u64 {
// For an extra challenge, don't use: // For an extra challenge, don't use:
// - recursion // - recursion
// Execute `rustlings hint iterators4` for hints. // Execute `rustlings hint iterators4` for hints.
(1..num+1).fold(1, |m, x| m * x)
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,21 +1,22 @@
// traits1.rs // traits1.rs
// Time to implement some traits! // Time to implement some traits!
// //
// Your task is to implement the trait // Your task is to implement the trait
// `AppendBar' for the type `String'. // `AppendBar' for the type `String'.
// //
// The trait AppendBar has only one function, // The trait AppendBar has only one function,
// which appends "Bar" to any object // which appends "Bar" to any object
// implementing this trait. // implementing this trait.
// I AM NOT DONE
trait AppendBar { trait AppendBar {
fn append_bar(self) -> Self; fn append_bar(self) -> Self;
} }
impl AppendBar for String { impl AppendBar for String {
//Add your code here fn append_bar(mut self) -> Self {
self.push_str("Bar");
self
}
} }
fn main() { fn main() {
@ -41,4 +42,4 @@ mod tests {
); );
} }
} }

View file

@ -1,25 +1,25 @@
// traits2.rs // traits2.rs
// //
// Your task is to implement the trait // Your task is to implement the trait
// `AppendBar' for a vector of strings. // `AppendBar' for a vector of strings.
// //
// To implement this trait, consider for // To implement this trait, consider for
// a moment what it means to 'append "Bar"' // a moment what it means to 'append "Bar"'
// to a vector of strings. // to a vector of strings.
// //
// No boiler plate code this time, // No boiler plate code this time,
// you can do this! // you can do this!
// I AM NOT DONE
trait AppendBar { trait AppendBar {
fn append_bar(self) -> Self; fn append_bar(self) -> Self;
} }
//TODO: Add your code here impl AppendBar for Vec<String> {
fn append_bar(mut self) -> Self {
self.push(String::from("Bar"));
self
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {