Commit d3698e1f authored by Mathieu Nivoliez's avatar Mathieu Nivoliez

correct typo and add implementations

parent 723ad301
......@@ -11,18 +11,16 @@ data:
---
Hello everyone! It's us again, Gaetan and Mathieu!
This is the second episode of *Tell me a story I could code*.
At our surprise, the concept worked more than we hope and you, YES YOU, have been very prolific!
We thank the people involved at the end ;)
At our surprise, the concept worked more than we hope, and you, YES YOU, have been very prolific!
## "What was proposed?"
In shrot, a movement, a dialog and the appartition of a dragon was proposed.
In short, a movement, a dialog and the apparition of a dragon was proposed.
Here is how we have proceed.
First, we have now a dragon, which is different from a human, but it is still a character.
Second, characters can speak to each over.
Third, we got a sense of location which character can go to or run away from.
First, we have now a dragon, which is still a character but different from a human.
Second, characters can speak to each other.
Third, characters can go to or come from location.
So, let's rename our `Character` structure to `Human`:
```rust
......@@ -77,7 +75,114 @@ pub trait Character {
}
```
Notice that the name inside `Character` was before a `&'static str` (meaning a [`String` slice](https://doc.rust-lang.org/std/primitive.str.html) with a `static` lifetime) and have been change for a `String`. The reason for that it is easier to work with the `String` and do not require to care about the lifetime (the "scope" in which the variable lives) in case we have to return it in a fashion manner. Also, note that using `static` mean that the lifetime of the reference is the same as the application lifetime.
Then, we can implement those `trait`s to `Character`
```rust
#[derive(Debug, Clone)]
pub struct Human {
pub name: String,
pub money: i32,
}
impl Human {
pub fn find(&mut self, item: Item) {
match item {
Item::Money(amount) => {
self.money += amount;
println!("Oh {} penny! \" said {}.\"", amount, self.name);
}
}
}
}
impl Character for Human {
fn new(name: &str) -> Self {
println!("There was a man named {}.", name);
Human {
name: name.to_owned(),
money: 0,
}
}
// Character needs to get back to another
fn move_to<T: Localizable>(&self, location: &T) {
println!("{} move to {}", self.name, location.get_location());
}
fn says_to<T: Character>(&self, other: &T, msg: &str) {
println!("\"{}\" said {} to {}", msg, self.name, other.get_name());
}
fn get_name(&self) -> &str {
self.name.as_str()
}
fn run_away_from<T: Localizable>(&self, location: &T) {
println!("{} run away from {}", self.name, location.get_location());
}
}
impl Localizable for Human {
fn get_location(&self) -> String {
self.name.clone()
}
}
```
and for `Dragon`
```rust
#[derive(Debug, Clone)]
pub struct Dragon {
pub name: String,
}
impl Dragon {
pub fn fly_toward<T: Localizable>(&self, location: &T) {
println!(
"{} the dragon fly toward {}",
self.name,
location.get_location()
);
}
}
impl Localizable for Dragon {
fn get_location(&self) -> String {
format!("{} the dragon", self.name)
}
}
impl Character for Dragon {
fn new(name: &str) -> Self {
println!("There was a dragon named {}.", name);
Dragon {
name: name.to_owned(),
}
}
fn move_to<T: Localizable>(&self, location: &T) {
println!("{} move to {}", self.name, location.get_location());
}
fn says_to<T: Character>(&self, other: &T, msg: &str) {
println!("\"{}\" said {} to {}", msg, self.name, other.get_name());
}
fn get_name(&self) -> &str {
&self.name
}
fn run_away_from<T: Localizable>(&self, location: &T) {
println!(
"How that, {} the dragon is running away from {}",
self.name,
location.get_location()
);
}
}
```
Note that we specified that only `Human` can find `Item` and that only `Dragon` can fly, so far.
Also note that the name inside `Character` was before a `&'static str` (meaning a [`String` slice](https://doc.rust-lang.org/std/primitive.str.html) with a `static` lifetime) and have been change for a `String`. The reason for that it is easier to work with the `String` and do not require to care about the lifetime (the "scope" in which the variable lives) in case we have to return it in a fashion manner. Also, note that using `static` mean that the lifetime of the reference is the same as the application lifetime.
Now the output looks like:
```bash
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment