RustLings II
Tags: functional, rust, learning
Published: 2021-10-01 18:05:18

Willkommen!!

Sorry is German :)

Re-writing the post, I've learned that rust is **expression-based** language HAHAHAH and I was like a friend does "AAAJJJAAAAAMMMMMMM...." that is a well I don't know how to translate it.

So what is an expression-based language??? Well I searched it on DuckDuckGo ;)

and for me the best result was the wikipedia page hahahha, yeah so this is what it says:
> An expression-oriented programming language is a programming language in which every (or nearly every) construction is an expression and thus yields a value.

But... so this means that everything or nearly every construction is an expression and it produces a value. Wish you understood it :V

Rust is made of Statements and Expressions
> _Statements_ are _Instructions_ that perform some action and do not return a value. _Expressions_ evaluate to a resulting value. (from docs)

Maybe with an example???
So in the book (that are the docs) it has this example:
```rs
let x = (let y = 6);
```
So here we would say that `x=6` and `y=6`, no?

Well this is not true with rust and there is an explanation... the errors in terminal... Ok no HAHAHAHHAHA
I was kidding.

You cannot do this in Rust and the reason is because the `let` keyword is a Statement, and as the book says, a Statement does not return a value!!!!
So it is impossible to do that in Rust!!

Then we have Expression, as the book says, those "evaluate to a resulting value", we could say that is something like "return a value". So... how can we do the last code and add 5 to work???
We can use `blocks`, this are statements you can have a piece of code, and return a value.
```rs
let y = 5
let x = {
  let y = 10;
  y + 5 // returns the value (also is an expression)
}
// if you print "y", you get 5 not 10, go and search why :D
```
Also, *Expressions DO NOT INCLUDE SEMICOLONS AT THE END*, and this is important.
Other Expressions ->
- calling a function
- calling a macro
- `let x = 6`, here `6` is an Expression, because `6` evaluates to `6` (AHAAHHAHAHAHAHAHA, love this explanations from the doc)

PD1: I think there are more, but I cannot think of them right now, those are examples from the docs, but everything that can evaluate to a value, is an Expression, otherwise an Instruction or Statement.

So that was the difficult part of this chapter, wish you understood everything and I wrote it well. Anything tell me :D

Here you have the easy part uwu

You create a function like with `fn` keyword
```rs
fn bonjour() {
  // If you've watched Modern Family, you will understand it :D
  println!("Yelooouuuhhh!!!");
}
```
if the function does not specify the return data type, then it returns nothing. You specify a function to return something like this:
```rs
fn example () -> i8 {}
```

Now as we are not in python, we need to specify the type of the parameter ([doc](https://doc.rust-lang.org/book/ch03-02-data-types.html)).

```rs
fn new_user (id: i32) {
  println!("New user {}", id);
}
```

In rust you can return the value in 2 ways.

1 -> without the `return` keyword
```rs
fn is_even(num: i32) -> bool {
    num % 2 == 0
}
```
And.... why does this work??? Well... very easy, you are specifying the type you want to return and you have no `;` in the instruction. Which means, as we said later, that this is an Expression so it evaluates a value.

Another example
```rs
fn uwu(n: i8) -> i8 {
  if (n < 19)
    n
  else
    n%20
```

2. -> using `return`
You just use the keyword and `;`
```rs
fn is_even(num: i32) -> bool {
    return num % 2 == 0;
}
```

PD: I have read the docs like 10 times to understand that jajajaj
![reading](https://media.giphy.com/media/NFA61GS9qKZ68/giphy.gif)

---

## Follow me!
(I liked more the red bird :_()
🐦 [Twitter](https://twitter.com/blit12_)
🐙 [GitHub](https://github.com/blitty-codes)
👥 [LinkdIn](https://www.linkedin.com/in/blitty/)

---