mirror of https://github.com/01-edu/public.git
Merge pull request #1062 from 01-edu/update-description-of-easy_traits-in-01-899
fix easy_traits
This commit is contained in:
commit
a9e04d8e88
|
@ -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
|
||||
$
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue