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!
// Use your knowledge of generics to fix it.
// 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

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

View file

@ -5,15 +5,15 @@
// Make the necessary code changes to support alphabetical report cards, thereby making the second
// test pass.
use std::fmt::Display;
// I AM NOT DONE
pub struct ReportCard {
pub grade: f32,
pub struct ReportCard<T> {
pub grade: T,
pub student_name: String,
pub student_age: u8,
}
impl ReportCard {
impl<T:Display> ReportCard<T> {
pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade)
}
@ -37,7 +37,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

@ -7,8 +7,6 @@
// Execute `rustlings hint iterators3` to get some hints!
// Have fun :-)
// I AM NOT DONE
#[derive(Debug, PartialEq, Eq)]
pub enum DivisionError {
NotDivisible(NotDivisibleError),
@ -70,12 +68,11 @@ mod tests {
}
// Iterator exercises using your `divide` function
/*
#[test]
fn result_with_list() {
let numbers = vec![27, 297, 38502, 81];
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])");
}
@ -83,8 +80,7 @@ mod tests {
fn list_of_results() {
let numbers = vec![27, 297, 38502, 81];
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)]");
}
*/
}

View file

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

View file

@ -8,14 +8,15 @@
// 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(mut self) -> Self {
self.push_str("Bar");
self
}
}
fn main() {

View file

@ -10,16 +10,16 @@
// 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) -> Self {
self.push(String::from("Bar"));
self
}
}
#[cfg(test)]
mod tests {