DEV Community

Cover image for RustLings II
Blitty
Blitty

Posted on

RustLings II

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:

let x = (let y = 6);
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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

fn bonjour() {
  // If you've watched Modern Family, you will understand it :D
  println!("Yelooouuuhhh!!!");
}
Enter fullscreen mode Exit fullscreen mode

if the function does not specify the return data type, then it returns nothing. You specify a function to return something like this:

fn example () -> i8 {}
Enter fullscreen mode Exit fullscreen mode

Now as we are not in python, we need to specify the type of the parameter (doc).

fn new_user (id: i32) {
  println!("New user {}", id);
}
Enter fullscreen mode Exit fullscreen mode

In rust you can return the value in 2 ways.

1 -> without the return keyword

fn is_even(num: i32) -> bool {
    num % 2 == 0
}
Enter fullscreen mode Exit fullscreen mode

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

fn uwu(n: i8) -> i8 {
  if (n < 19)
    n
  else
    n%20
Enter fullscreen mode Exit fullscreen mode
  1. -> using return You just use the keyword and ;
fn is_even(num: i32) -> bool {
    return num % 2 == 0;
}
Enter fullscreen mode Exit fullscreen mode

PD: I have read the docs like 10 times to understand that jajajaj
reading


Follow me!

(I liked more the red bird :_()
🐦 Twitter
🐙 GitHub
👥 LinkdIn


Discussion (1)

Collapse
captainyossarian profile image
yossarian

I see article about Rust - I upvote