From 584c8468440add8e7b184269e5794fbe1e5c2600 Mon Sep 17 00:00:00 2001 From: davidrobert99 Date: Thu, 21 Apr 2022 17:09:51 +0100 Subject: [PATCH] fix easy_traits --- subjects/easy_traits/README.md | 49 ++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/subjects/easy_traits/README.md b/subjects/easy_traits/README.md index 929127e2..b6c9e5fe 100644 --- a/subjects/easy_traits/README.md +++ b/subjects/easy_traits/README.md @@ -2,25 +2,33 @@ ### Instructions -Your task is to implement the trait `AppendStr` for the type String, and `AppendVec` for a vector of strings. +Your task is to implement the trait `AppendStr` for the type StringValue. + +The trait `AppendStr` has the following functions: + +- `append_str`, that appends to the value of the structure a new_str of type String +- `append_number`, that appends to the value of the structure a new_number of type f64 +- `remove_punctuation_marks`, that removes from the value of the structure the following punctuation marks: `. , ? !` -The trait `AppendStr` has only one function, which is appending `"world"` to any object implementing this trait. -And `AppendVec` will append `"three"` to a vector of strings. ### Expected Function ```rust -trait AppendStr { - fn append_str(self) -> Self; +#[derive(Clone)] +struct StringValue { + value: String, } -trait AppendVec { - fn append_vec(&mut self) -> Self; -} +trait AppendStr { + fn append_str(self, new_str: String) -> Self; -impl AppendStr for String {} + fn append_number(self, new_number: f64) -> Self; -impl AppendVec for Vec {} + fn remove_punctuation_marks(self) -> Self; +} + +impl AppendStr for StringValue { +} ``` ### Usage @@ -28,14 +36,20 @@ impl AppendVec for Vec {} Here is a program to test your function. ```rust -use easy_traits::easy_traits; +use easy_traits::*; fn main() { - let s = String::from("hello ").append_str(); - println!("{}", s); + let mut str_aux = StringValue { + value: String::from("hello"), + }; + + println!("Before append: {}", str_aux.value); + + str_aux.append_str(String::from(" there!")); + println!("After append: {}", str_aux.value); - let l = vec![String::from("one"), String::from("two")].append_vec(); - println!("{:?}", l); + str_aux.remove_punctuation_marks(); + println!("After removing punctuation: {}", str_aux.value); } ``` @@ -43,7 +57,8 @@ And its output ```console $ cargo run -hello world -["one", "two", "three"] +Before append: hello +After append: hello there! +After removing punctuation: hello there $ ```