|
|
@ -2,25 +2,33 @@ |
|
|
|
|
|
|
|
|
|
|
|
### Instructions |
|
|
|
### 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 |
|
|
|
### Expected Function |
|
|
|
|
|
|
|
|
|
|
|
```rust |
|
|
|
```rust |
|
|
|
trait AppendStr { |
|
|
|
#[derive(Clone)] |
|
|
|
fn append_str(self) -> Self; |
|
|
|
struct StringValue { |
|
|
|
|
|
|
|
value: String, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
trait AppendVec { |
|
|
|
trait AppendStr { |
|
|
|
fn append_vec(&mut self) -> Self; |
|
|
|
fn append_str(self, new_str: String) -> Self; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl AppendStr for String {} |
|
|
|
fn append_number(self, new_number: f64) -> Self; |
|
|
|
|
|
|
|
|
|
|
|
impl AppendVec for Vec<String> {} |
|
|
|
fn remove_punctuation_marks(self) -> Self; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl AppendStr for StringValue { |
|
|
|
|
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
### Usage |
|
|
|
### Usage |
|
|
@ -28,14 +36,20 @@ impl AppendVec for Vec<String> {} |
|
|
|
Here is a program to test your function. |
|
|
|
Here is a program to test your function. |
|
|
|
|
|
|
|
|
|
|
|
```rust |
|
|
|
```rust |
|
|
|
use easy_traits::easy_traits; |
|
|
|
use easy_traits::*; |
|
|
|
|
|
|
|
|
|
|
|
fn main() { |
|
|
|
fn main() { |
|
|
|
let s = String::from("hello ").append_str(); |
|
|
|
let mut str_aux = StringValue { |
|
|
|
println!("{}", s); |
|
|
|
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(); |
|
|
|
str_aux.remove_punctuation_marks(); |
|
|
|
println!("{:?}", l); |
|
|
|
println!("After removing punctuation: {}", str_aux.value); |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
@ -43,7 +57,8 @@ And its output |
|
|
|
|
|
|
|
|
|
|
|
```console |
|
|
|
```console |
|
|
|
$ cargo run |
|
|
|
$ cargo run |
|
|
|
hello world |
|
|
|
Before append: hello |
|
|
|
["one", "two", "three"] |
|
|
|
After append: hello there! |
|
|
|
|
|
|
|
After removing punctuation: hello there |
|
|
|
$ |
|
|
|
$ |
|
|
|
``` |
|
|
|
``` |
|
|
|