tell-me-a-story-i-could-code-2.md 3.65 KB
Newer Older
Mathieu Nivoliez's avatar
Mathieu Nivoliez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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);
}
```
Mathieu Nivoliez's avatar
Mathieu Nivoliez committed
56
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.
Mathieu Nivoliez's avatar
Mathieu Nivoliez committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

```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
Mathieu Nivoliez's avatar
Mathieu Nivoliez committed
94
Bob run away from Igorrr the dragon
Mathieu Nivoliez's avatar
Mathieu Nivoliez committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
"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