Merge pull request #1062 from 01-edu/update-description-of-easy_traits-in-01-899

fix easy_traits
This commit is contained in:
David Mata 2022-04-26 10:41:34 +01:00 committed by GitHub
commit a9e04d8e88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 17 deletions

View File

@ -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
#[derive(Clone)]
struct StringValue {
value: String,
}
trait AppendStr {
fn append_str(self) -> Self;
fn append_str(self, new_str: String) -> Self;
fn append_number(self, new_number: f64) -> Self;
fn remove_punctuation_marks(self) -> Self;
}
trait AppendVec {
fn append_vec(&mut self) -> Self;
impl AppendStr for StringValue {
}
impl AppendStr for String {}
impl AppendVec for Vec<String> {}
```
### Usage
@ -28,14 +36,20 @@ impl AppendVec for Vec<String> {}
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"),
};
let l = vec![String::from("one"), String::from("two")].append_vec();
println!("{:?}", l);
println!("Before append: {}", str_aux.value);
str_aux.append_str(String::from(" there!"));
println!("After append: {}", str_aux.value);
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
$
```