Commit f792ea72 authored by Mathieu Nivoliez's avatar Mathieu Nivoliez

Add episode 2

parent 6042e04e
......@@ -5,7 +5,7 @@ is_draft: false
data:
dsq_thread_id: ""
lang: en
author: "Mathieu Nivoliez & Gaetan Baton de Noyer"
author: "Mathieu Nivoliez & Gaetan Bayon de Noyer"
comments: true
img_cover: ""
---
......
title: "Tell me a story I could code #2"
layout: post.liquid
is_draft: true
data:
dsq_thread_id: ""
lang: en
author: "Mathieu Nivoliez & Gaetan Bayon de Noyer"
comments: true
img_cover: ""
---
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 ;)
## "What was proposed?"
In shrot, a movement, a dialog and the appartition 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.
So, let's rename our `Character` structure to `Human`:
```rust
struct Human {
name: String,
money: i32,
}
```
and then create the `Dragon` struct:
```rust
struct Dragon {
name: String,
}
```
Next step abstract the Character behaviour:
```rust
pub trait Character {
fn new(name: &str) -> Self;
fn says_to(&self, other: &Self, msg: &str);
fn move_to(&self, location: &Self);
fn get_name(&self) -> &str;
fn run_away_from(&self, location: &Self);
}
```
But as it, the only `Location` a `Charater` can go to or run From is another `Charater`. So let say that we want `Character` able to do that with a `Localisable` struct.
```rust
pub trait Localizable {
fn get_location(&self) -> String;
}
```
We can then adapt the `Character` struct like that to tell that some metho will use a type `T` that `impl`ements `Localizable`:
```rust
pub trait Character {
fn new(name: &str) -> Self;
fn says_to<T: Character>(&self, other: &T, msg: &str);
fn move_to<T: Localizable>(&self, location: &T);
fn get_name(&self) -> &str;
fn run_away_from<T: Localizable>(&self, location: &T);
}
```
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.
Now the output looks like:
```bash
Once upon a time...
There was a man named Bob.
Oh 1 penny! " said Bob."
There was a man named Joe.
Bob move to Joe
"Dude! Look what I found!" said Bob to Joe
"You should invest the penny in the Sheriff of Nottingham's Bank" said Joe to Bob
"But I already have invested money in the bank, I want to buy a horse!" said Bob to Joe
There was a dragon named Igorrr.
Igorrr the dragon fly toward Bob
"The shiny thing belongs to me" said Igorrr to Bob
"Never! Founders keepers, you Reptilebug!" said Bob to Igorrr
```
The full code is available [here](https://gitlab.deep-nope.me/mnivoliez/tell-me-story-i-could-code).
More on:
* [Slice](https://doc.rust-lang.org/book/second-edition/ch04-03-slices.html)
* [Trait](https://doc.rust-lang.org/book/second-edition/ch10-02-traits.html)
* [Lifetime](https://doc.rust-lang.org/book/second-edition/ch10-03-lifetime-syntax.html)
Special thanks to:
* Julian Chan
* Jason C. McDonald
* Faizan Rabbani
* Valentin Silvestre
* Sauban
* Salim Mahboubi
from the [dev.to post discussion](https://dev.to/mnivoliez/tell-me-a-story-i-could-code-1ho7/comments) for adding element to the story.
Same as last time, we are counting on you for the follow up :p
--
Gaetan and Mathieu
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