diff --git a/lessons/en/chapter_1.yaml b/lessons/en/chapter_1.yaml
index dc2137a3a..376432b5a 100644
--- a/lessons/en/chapter_1.yaml
+++ b/lessons/en/chapter_1.yaml
@@ -12,6 +12,24 @@
Once you get familiar with Rust, you can call yourself a **Rustacean**. That's
how people who use, contribute or are interested in Rust call themself.
+- title: What is Rust?
+ content_markdown: >
+ Rust is a systems programming language that is known for its emphasis on
+ safety, performance and concurrency.
+ Rust is designed to provide low-level control over system resources
+ without sacrificing high-level abstractions.
+
+ What does Rust have different from other programming languages.
+ - ownership and borrowing
+ - lifetime
+ - pattern matching
+ - concurrency without data races
+ - traits
+ - no null or garbage collection
+ - macros
+ - cargo
+ - community and ecosystem
+ - UTF-8 and Unicode text
- title: The Rust Playground
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20println!(%22Welcome%20to%20the%20playground!%20You%20can%20modify%20the%20code%20in%20here.%22)%3B%0A%7D%0A
@@ -19,9 +37,37 @@
This tour uses an interactive coding tool from [Rust
Playground](https://play.rust-lang.org).
-
It's a great way to play around with Rust and show others your creativity
and challenges!
+
+- title: What is println! ?
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++println%21%28%29%3B%0A++++print%21%28%22There+is+an+empty+line+above+this.+%22%29%3B%0A++++print%21%28%22Isn%27t+it+great%3F%22%29%3B%0A++++println%21%28%29%3B+++++%2F%2F+new+line+at+the+end+of+stdout%0A%7D%0A
+ content_markdown: >
+ Unlike other programming languages, where there is a function for
+ writing text in `stdout` (standard output), Rust uses the `macros` **println!** and **print!**.
+ We'll talk about `macros` later.
+
+ > `println!` will display using a new line character `\n` at the end of the string
+ > `print!` will not use the `\n` at the end of the text
+- title: How to compile?
+ content_markdown: >
+ On Linux / MacOS systems:
+ ```
+ $ rustc main.rs
+ $ ./main
+ ```
+
+ On Windows systems?
+ ```
+ > rustc main.rs
+ > .\main.exe
+ ```
+
+ | Operating System (OS) | terminal commands |
+ | --------------------- | --------------------------------- |
+ | Linux / MacOS | $ rustc main.rs
$ ./main |
+ | Windows | > rustc main.rs
> ./main.exe |
- title: Variables
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20%2F%2F%20rust%20infers%20the%20type%20of%20x%0A%20%20%20%20let%20x%20%3D%2013%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%0A%20%20%20%20%2F%2F%20rust%20can%20also%20be%20explicit%20about%20the%20type%0A%20%20%20%20let%20x%3A%20f64%20%3D%203.14159%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%0A%20%20%20%20%2F%2F%20rust%20can%20also%20declare%20and%20initialize%20later%2C%20but%20this%20is%20rarely%20done%0A%20%20%20%20let%20x%3B%0A%20%20%20%20x%20%3D%200%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%7D%0A
@@ -67,6 +113,29 @@
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn+main%28%29+%7B%0A++++let+x+%3D+12%3B+%2F%2F+by+default+this+is+i32%0A++++let+a+%3D+12u8%3B%0A++++let+b+%3D+4.3%3B+%2F%2F+by+default+this+is+f64%0A++++let+c+%3D+4.3f32%3B%0A++++let+d+%3D+%27r%27%3B+%2F%2F+unicode+character%0A++++let+ferris+%3D+%27%F0%9F%A6%80%27%3B+%2F%2F+also+a+unicode+character%0A++++let+bv+%3D+true%3B%0A++++let+t+%3D+%2813%2C+false%29%3B%0A++++let+sentence+%3D+%22hello+world%21%22%3B%0A++++println%21%28%0A++++++++%22%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D%22%2C%0A++++++++x%2C+a%2C+b%2C+c%2C+d%2C+ferris%2C+bv%2C+t.0%2C+t.1%2C+sentence%0A++++%29%3B%0A%7D%0A
content_markdown: >
+ Basic data types:
+ - bool
+ - u8
+ - u16
+ - u32
+ - u64
+ - u128
+ - i8
+ - i16
+ - i32
+ - i64
+ - i128
+ - usize
+ - isize
+ - f32
+ - f64
+
+ | u/i | sign | sign | sign|
+ | --- | ---------------- | ---------------------------------- | --- |
+ | `u` | unsigned numbers | positive numbers only | + |
+ | `i` | signed numbers | both negative and positive numbers | ± |
+
+
Rust has a variety of familiar types:
@@ -105,6 +174,9 @@
Numeric types can be explicitly specified by appending the type to the end
of the number (e.g. `13u32`, `2u8`).
+
+ > cannot apply unary operator '-' on `u8`, `u16`, `u32`, `u64`, `u128` and `usize`
+
- title: Basic Type Conversion
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20a%20%3D%2013u8%3B%0A%20%20%20%20let%20b%20%3D%207u32%3B%0A%20%20%20%20let%20c%20%3D%20a%20as%20u32%20%2B%20b%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20c)%3B%0A%0A%20%20%20%20let%20t%20%3D%20true%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20t%20as%20u8)%3B%0A%7D%0A
@@ -136,9 +208,50 @@
Constant names are always in `SCREAMING_SNAKE_CASE`.
+- title: Collections
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=use+std%3A%3Acollections%3A%3AHashMap%3B%0Ause+std%3A%3Acollections%3A%3AHashSet%3B%0A%0Afn+main%28%29+%7B%0A%0A++++%2F%2F+array%0A++++let+array%3A+%5Bi32%3B+3%5D+%3D+%5B1%2C+2%2C+3%5D%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+array%29%3B%0A++++%0A++++%2F%2F+vector%0A++++let+vector%3A+Vec%3Ci32%3E+%3D+vec%21%5B1%2C+2%2C+3%5D%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+vector%29%3B%0A%0A++++%2F%2F+slice+%28from+other+collection%29%0A++++let+slice+%3D+%26array%5B1..3%5D%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+slice%29%3B%0A++++%0A++++%2F%2F+string%0A++++let+string%3A+String+%3D+String%3A%3Afrom%28%22Hello%2C+Rust%21%22%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+string%29%3B%0A++++%0A++++%2F%2F+tuple%0A++++let+tuple%3A+%28i32%2C+f64%2C+u8%29+%3D+%2842%2C+3.14%2C+5%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+tuple%29%3B%0A%0A++++%2F%2F+map%0A++++let+mut+map+%3D+HashMap%3A%3Anew%28%29%3B%0A++++map.insert%28%22one%22%2C+1%29%3B%0A++++map.insert%28%22two%22%2C+2%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+map%29%3B%0A++++%0A++++%2F%2F+set%0A++++let+mut+set+%3D+HashSet%3A%3Anew%28%29%3B%0A++++set.insert%281%29%3B%0A++++set.insert%282%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+set%29%3B%0A%7D%0A
+ content_markdown: >
+ Rust provides a variety of collection types that allow you to store and
+ manipulate data.
+
+ The main collections in Rust:
+ - Arrays
+ - Vectors
+ - Slices
+ - Strings
+ - Tuple
+ - Hash Maps
+ - Hash Sets
+
+ We will discuss each of them latter on.
+- title: Printing with `{:?}`
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A%0A++++let+my_vector+%3D+vec%21%5B1%2C+2%2C+3%5D%3B%0A++++%2F%2F+println%21%28%22%7B%7D%22%2C+my_vector%29%3B+++%2F%2F+will+generate+an+error%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+my_vector%29%3B%0A++++println%21%28%22%7B%3A%23%3F%7D%22%2C+my_vector%29%3B+++%2F%2F+debug+mode%0A++++%0A++++let+x%3A+i32+%3D+-12%3B%0A++++println%21%28%22%7B%7D%22%2C+x%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+x%29%3B%0A++++println%21%28%22%7B%3A%23%3F%7D%22%2C+x%29%3B%09%0A%7D%0A
+ content_markdown: >
+ Notice that when displaying a collection, `println!("{}", collection);`
+ doesn't work.
+
+ In Rust, the `:?` is a format specifier used with the `Debug` trait when
+ printing values / collections of values.
+ When you use `println!("{:?}", x);`, the Rust compiler formats the variable
+ accordingly.
+
+ This is particularly useful for `collections` like vectors, arrays, structs
+ and enums.
+
+ `{:#?}` will print them in a way that is designed to be informative during
+ debugging.
+
+
+ | Printing | basic type | collection |
+ | --------------------- | ---------- | ---------- |
+ | println!("{}", x); | ✅ | ❌ |
+ | println!("{:?}", x); | ✅ | ✅ |
+ | println!("{:#?}", x); | ✅ | ✅ |
- title: Arrays
code: >-
- https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20nums%3A%20%5Bi32%3B%203%5D%20%3D%20%5B1%2C%202%2C%203%5D%3B%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20nums)%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20nums%5B1%5D)%3B%0A%7D%0A
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++let+nums%3A+%5Bi32%3B+5%5D+%3D+%5B1%2C+2%2C+3%2C+4%2C+5%5D%3B%0A%0A++++println%21%28%22%7B%7D%22%2C+nums%5B1%5D%29%3B%0A%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+nums%29%3B+%2F%2F+%3A%3F+for+displaying+collections%0A++++println%21%28%22%7B%3A%23%3F%7D%22%2C+nums%29%3B+%2F%2F+%3A%23%3F+displaying+a+collection+in+debug+mode%0A%0A++++for+el+in+nums+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+el%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%0A++++for+i+in+0..%3D%28nums.len%28%29+-+1%29+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+nums%5Bi%5D%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%0A++++for+i+in+0..nums.len%28%29+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+nums%5Bi%5D%29%3B%0A++++%7D%0A++++println%21%28%22%22%29%3B%0A%7D%0A
content_markdown: >
An *array* is a **fixed length collection** of data elements all of the same
type.
@@ -154,9 +267,42 @@
Collections with a dynamic length, often called dynamic or variable arrays, are
introduced in a later chapter about **Vectors**.
+- title: Vectors
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++%2F%2F+vec%21%5B%5D+is+a+macro%0A++++let+v1+%3D+vec%21%5B1%2C+2%2C+3%2C+4%2C+5%5D%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+v1%29%3B%0A++++%0A++++let+mut+v2+%3D+Vec%3A%3Anew%28%29%3B%0A++++v2.push%281%29%3B%0A++++v2.push%282%29%3B%0A++++v2.push%283%29%3B%0A++++%0A++++%2F%2F+Using+a+for+loop%0A++++for+el+in+%26v1+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+el%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A++++%0A++++%2F%2F+Using+iter%28%29+method%0A++++for+el+in+v1.iter%28%29+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+el%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A++++%0A++++for+i+in+0..v1.len%28%29+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+v1%5Bi%5D%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A++++%0A++++%0A++++%2F%2F+we%27ll+talk+about+Some+and+Result+later%0A++++if+let+Some%28element%29+%3D+v1.get%281%29+%7B%0A++++++++println%21%28%22Second+element%3A+%7B%7D%22%2C+element%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22Index+out+of+bounds.%22%29%3B%0A++++%7D%0A++++%0A%7D%0A
+ content_markdown: >
+ Vectors are on of the most flexible and commonly used collection types in Rust.
+
+ They represent a dynamic, growable array, and they are part of the standard
+
+ Rust library (`std::vec::Vec`). No need to `use` it. :)
+- title: Strings
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++let+str1+%3D+%22Hello%2C+world%22%3B+%2F%2F+%26str+Type%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+str1%29%3B%0A++++%0A++++let+mut+s1%3A+String+%3D+String%3A%3Afrom%28%22%22%29%3B%0A++++s1.push_str%28%22Hello%22%29%3B%0A++++s1.push_str%28%22%2C+world%21%22%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+s1%29%3B%0A++++%0A++++let+s2%3A+String+%3D+String%3A%3Afrom%28%22Rust%22%29%3B%0A++++let+s3%3A+String+%3D+s1+%2B+%26s2%3B++%2F%2F+s1+will+be+moved+to+s3%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+s3%29%3B%0A++++%2F%2F+println%21%28%22%7B%3A%3F%7D%22%2C+s1%29%3B++++%2F%2F+doesn%27t+work+%3A+s1+is+borrowed+by+s3%0A++++%0A++++let+s4%3A+String+%3D+s3.clone%28%29%3B%0A++++for+ch+in+s4.chars%28%29+%7B%0A++++++++print%21%28%22%7B%7D%22%2C+ch%29%3B%0A++++%7D%0A++++%0A%7D%0A
+ content_markdown: >
+ In Rust strings are UTF-8 encoded sequence of Unicode characters.
+
+ The String type, which is part of the standard library
+ (`std::string::String`), is the most commonly used string type in Rust.
+
+ Rust also has a primitive string type, `&str`, which represents a string slice.
+
+ We'll later discuss in detail about Strings.
+- title: Tuple
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++%2F%2F+Create+a+tuple%0A++++let+my_tuple+%3D+%2811%2C+%22Hello%2C+Rust%21%22%2C+3.14%29%3B%0A%0A++++%2F%2F+Access+elements+using+indexing%0A++++let+first_element+%3D+my_tuple.0%3B%0A++++let+second_element+%3D+my_tuple.1%3B%0A++++let+third_element+%3D+my_tuple.2%3B%0A%0A++++%2F%2F+Print+the+elements%0A++++println%21%28%22First%3A+%7B%7D%22%2C+first_element%29%3B%0A++++println%21%28%22Second%3A+%7B%7D%22%2C+second_element%29%3B%0A++++println%21%28%22Third%3A+%7B%7D%22%2C+third_element%29%3B%0A%7D%0A
+ content_markdown: >
+ `Tuple` represents an **immutable** collections of elements, that can have
+ different data types.
+
+ Since it's **immutable**, it cannot be modified after its creation.
+
+ The elements of a tuple can be accessed by index (indexing starts from 0),
+ using the `.` operator.
+
- title: Functions
code: >-
- https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20add(x%3A%20i32%2C%20y%3A%20i32)%20-%3E%20i32%20%7B%0A%20%20%20%20return%20x%20%2B%20y%3B%0A%7D%0A%0Afn%20subtract(x%3A%20i32%2C%20y%3A%20i32)%20-%3E%20i32%20%7B%0A%20%20%20%20x%20-%20y%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20println!(%2242%20%2B%2013%20%3D%20%7B%7D%22%2C%20add(42%2C%2013))%3B%0A%20%20%20%20println!(%2242%20-%2013%20%3D%20%7B%7D%22%2C%20subtract(42%2C%2013))%3B%0A%7D%0A
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+add%28x%3A+i32%2C+y%3A+i32%29+-%3E+i32+%7B%0A++++return+x+%2B+y%3B%0A%7D%0A%0Afn+subtract%28x%3A+i32%2C+y%3A+i32%29+-%3E+i32+%7B%0A++++x+-+y%0A%7D%0A%0Afn+last_digit%28mut+x%3A+i32%29+-%3E+u8+%7B%0A++++%0A++++if+x+%3C+0+%7B%0A++++++++while+x+%3C+-9+%7B%0A++++++++++++x+%3D+x+%2F+10%3B%0A++++++++%7D%0A++++++++return+%28-x%29+as+u8%3B%0A++++%0A++++%7D+else+%7B%0A++++++++while+x+%3E+9+%7B%0A++++++++++++x+%3D+x+%2F+10%3B%0A++++++++%7D%0A++++++++return+x+as+u8%3B%0A++++%7D%0A%7D%0A%0Afn+main%28%29+%7B%0A++++println%21%28%2242+%2B+13+%3D+%7B%7D%22%2C+add%2842%2C+13%29%29%3B%0A++++println%21%28%2242+-+13+%3D+%7B%7D%22%2C+subtract%2842%2C+13%29%29%3B%0A++++%0A++++let+mut+nr+%3D+612129%3B%0A++++println%21%28%22last+digit+of+%7B%7D+%3D+%7B%7D%22%2C+nr%2C+last_digit%28nr%29%29%3B%0A%0A++++nr+%3D+-31324%3B%0A++++println%21%28%22last+digit+of+%7B%7D+%3D+%7B%7D%22%2C+nr%2C+last_digit%28nr%29%29%3B%0A%7D%0A
content_markdown: >
A function has zero or more parameters.
@@ -204,6 +350,28 @@
whats happening.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20make_nothing()%20-%3E%20()%20%7B%0A%20%20%20%20return%20()%3B%0A%7D%0A%0A%2F%2F%20the%20return%20type%20is%20implied%20as%20()%0Afn%20make_nothing2()%20%7B%0A%20%20%20%20%2F%2F%20this%20function%20will%20return%20()%20if%20nothing%20is%20specified%20to%20return%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20a%20%3D%20make_nothing()%3B%0A%20%20%20%20let%20b%20%3D%20make_nothing2()%3B%0A%0A%20%20%20%20%2F%2F%20Printing%20a%20debug%20string%20for%20a%20and%20b%0A%20%20%20%20%2F%2F%20Because%20it's%20hard%20to%20print%20nothingness%0A%20%20%20%20println!(%22The%20value%20of%20a%3A%20%7B%3A%3F%7D%22%2C%20a)%3B%0A%20%20%20%20println!(%22The%20value%20of%20b%3A%20%7B%3A%3F%7D%22%2C%20b)%3B%0A%7D%0A
+- title: Macros
+ content_markdown: >
+ A `macro` is an abstraction from another code, that allows developers to
+ define reusable patterns and reduce duplicate code.
+
+ In Rust, there are two types of `macros`:
+ - declarative
+ - procedural
+- title: Declarative macros
+ content_markdown: >
+ Declarative macros, such as **print!**, **format!** or **todo!**
+ are invoked using the syntax `macro_name!(....)`.
+
+ The `!` indicates that it's a macro invocation
+ rather than a regular function call.
+- title: Procedural macros
+ content_markdown: >
+ Procedural macros operate on the abstract syntax tree (AST) of Rust code and
+ generate or modify code accordingly. They are more powerful and flexible
+ than declarative macros but are also more complex.
+
+ Examples: `#[derive(Debug)]`, `#[test]`, `#[derive(Serialize)]`
- title: Chapter 1 - Conclusion
content_markdown: >
Nice job so far! The basics of Rust aren't so bad, right? We're
diff --git a/lessons/en/chapter_2.yaml b/lessons/en/chapter_2.yaml
index af4974bef..3a7800870 100644
--- a/lessons/en/chapter_2.yaml
+++ b/lessons/en/chapter_2.yaml
@@ -67,7 +67,7 @@
and including an end number.
code: >-
- https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20for%20x%20in%200..5%20%7B%0A%20%20%20%20%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20for%20x%20in%200..%3D5%20%7B%0A%20%20%20%20%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%20%20%20%20%7D%0A%7D%0A
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A%0A++++for+x+in+0..5+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+x%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%0A++++for+x+in+0..%3D5+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+x%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%0A++++let+nums+%3A+%5Bi32%3B+5%5D+%3D+%5B10%2C+-1%2C+9%2C+-80%2C+1%5D%3B%0A++++%0A++++print%21%28%22%7B%7D+%3A+%22%2C+stringify%21%28nums%29%29%3B%0A++++for+el+in+nums+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+el%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%0A++++print%21%28%22%7B%7D+%3A+%22%2C+stringify%21%28nums%29%29%3B%0A++++for+idx+in+0+..+nums.len%28%29+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+nums%5Bidx%5D%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%7D%0A
- title: match
content_markdown: >
Miss your switch statement? Rust has an incredibly useful keyword
diff --git a/lessons/en/chapter_4.yaml b/lessons/en/chapter_4.yaml
index f55c2bf47..08c9964f6 100644
--- a/lessons/en/chapter_4.yaml
+++ b/lessons/en/chapter_4.yaml
@@ -181,6 +181,44 @@
Be a good rustacean and properly use `match` when you can!
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20do_something_that_might_fail(i%3A%20i32)%20-%3E%20Result%3Cf32%2C%20String%3E%20%7B%0A%20%20%20%20if%20i%20%3D%3D%2042%20%7B%0A%20%20%20%20%20%20%20%20Ok(13.0)%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20Err(String%3A%3Afrom(%22this%20is%20not%20the%20right%20number%22))%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20-%3E%20Result%3C()%2C%20String%3E%20%7B%0A%20%20%20%20%2F%2F%20concise%20but%20assumptive%20and%20gets%20ugly%20fast%0A%20%20%20%20let%20v%20%3D%20do_something_that_might_fail(42).unwrap()%3B%0A%20%20%20%20println!(%22found%20%7B%7D%22%2C%20v)%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20this%20will%20panic!%0A%20%20%20%20let%20v%20%3D%20do_something_that_might_fail(1).unwrap()%3B%0A%20%20%20%20println!(%22found%20%7B%7D%22%2C%20v)%3B%0A%20%20%20%20%0A%20%20%20%20Ok(())%0A%7D%0A
+- title: if let
+ content_markdown: >
+ A much easier way to handle error propagation in Rust
+ is to use the `if let` block. It basically handles just
+ the only `match` branch that we are interested to get.
+
+ For instance, if we want to get the **error**,
+ `if let Err(..) = ..` will be used.
+
+ Otherwise, `if let Ok(..) = ..` will be chosen
+ to handle the **success** case.
+
+ `if let` can be used only for `Result` and `Option` data types.
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn+div%28x%3A+f64%2C+y%3A+f64%29+-%3E+Result%3Cf64%2C+String%3E+%7B%0A++++if+y+%3D%3D+0.0+%7B%0A++++++++return+Err%28String%3A%3Afrom%28%22Invalid+operation%3A+division+by+zero.%22%29%29%3B%0A++++%7D%0A++++return+Ok%28x+%2F+y%29%3B%0A%7D%0A%0A%0Afn+main%28%29+%7B%0A++++if+let+Ok%28res%29+%3D+div%2810.0%2C+1.0%29+%7B%0A++++++++println%21%28%22%7B%7D%22%2C+res%29%3B++++++++%2F%2F+will+be+displayed%0A++++%7D%0A++++%0A++++if+let+Err%28err%29+%3D+div%2810.0%2C+1.0%29+%7B%0A++++++++println%21%28%22%7B%7D%22%2C+err%29%3B++++++++%2F%2F+will+NOT+be+displayed%0A++++%7D%0A++++%0A%0A++++if+let+Ok%28res%29+%3D+div%2810.0%2C+0.0%29+%7B%0A++++++++println%21%28%22%7B%7D%22%2C+res%29%3B++++++++%2F%2F+will+be+displayed%0A++++%7D%0A++++if+let+Err%28err%29+%3D+div%2810.0%2C+0.0%29+%7B%0A++++++++println%21%28%22%7B%7D%22%2C+err%29%3B++++++++%2F%2F+will+NOT+be+displayed%0A++++%7D%0A%7D%0A
+- title: Extracting a Result
+ content_markdown: >
+ Another brilliant way to extract a `Result { Ok(T), Err(E), }`
+ is to use the functions `.ok()` and `.err()`, representing the specific variant.
+
+ Let's assume we are working with a function whose return type is `Result`, just like in the example.
+ Using `.ok()` and `.err()` on it will return an `Option`:
+ - `Some` if the original function had something to return, for results like `Result`.
+ - `None` if the original function returned either a `Err(())` or an `Ok(())`, for results like `Result<(), ()>`, `Result` or `Result<(), i32>`
+
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn+div1%28x%3A+f64%2C+y%3A+f64%29+-%3E+Result%3Cf64%2C+String%3E+%7B%0A++++if+y+%3D%3D+0.0+%7B%0A++++++++return+Err%28String%3A%3Afrom%28%22Invalid+operation%3A+division+by+zero.%22%29%29%3B%0A++++%7D%0A++++return+Ok%28x+%2F+y%29%3B%0A%7D%0A%0A%0Afn+div2%28x%3A+f64%2C+y%3A+f64%29+-%3E+Result%3Cf64%2C+%28%29%3E+%7B%0A++++if+y+%3D%3D+0.0+%7B%0A++++++++%2F%2F+notice+that+the+error+returns+%60None%60%0A++++++++%2F%2F+therefore+will+be+pattern-matched+using+%60%28%29%60%0A++++++++return+Err%28%28%29%29%3B%0A++++%7D%0A++++return+Ok%28x+%2F+y%29%3B%0A%7D%0A%0Afn+main%28%29+%7B%0A++++let+good1+%3D+div1%2810.0%2C+1.0%29%3B%0A++++let+bad1+%3D+div1%2810.0%2C+0.0%29%3B%0A++++%0A++++%2F%2F+Using+.ok%28%29+will+extract+the+Ok+variant%0A++++%2F%2F+.ok%28%29+returns+an+Option%3A+Some%0A++++if+let+Some%28value%29+%3D+good1.ok%28%29+%7B%0A++++++++println%21%28%22Result+%28Ok%29%3A+%7B%7D%22%2C+value%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22Result+%28Err%29%22%29%3B%0A++++%7D%0A%0A++++%2F%2F+Using+.err%28%29+to+extract+the+Err+variant%0A++++%2F%2F+.err%28%29+returns+an+Option%3A+Some%0A++++if+let+Some%28err%29+%3D+bad1.err%28%29+%7B%0A++++++++println%21%28%22Error%3A+%7B%7D%22%2C+err%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22No+error+%28Ok%29%22%29%3B%0A++++%7D%0A++++%0A++++%0A++++let+good2+%3D+div2%2810.0%2C+1.0%29%3B%0A++++let+bad2+%3D+div2%2810.0%2C+0.0%29%3B%0A++++%0A++++%2F%2F+Using+.ok%28%29+will+extract+the+Ok+variant%0A++++%2F%2F+.ok%28%29+returns+an+Option%3A+Some%0A++++if+let+Some%28value%29+%3D+good2.ok%28%29+%7B%0A++++++++println%21%28%22Result+%28Ok%29%3A+%7B%7D%22%2C+value%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22Result+%28Err%29%22%29%3B%0A++++%7D%0A%0A++++%2F%2F+Using+.err%28%29+to+extract+the+Err+variant%0A++++%2F%2F+.err%28%29+returns+an+Option%3A+None%0A++++if+let+Some%28%28%29%29+%3D+bad2.err%28%29+%7B%0A++++++++println%21%28%22Error+occured%22%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22No+error+%28Ok%29%22%29%3B%0A++++%7D%0A%7D%0A
+- title: About panic!
+ content_markdown: >
+ In Rust, `panic!` is a macro used to stop the execution of the program
+ without a recoverable error. When panic occurs, the program immediately
+ stops, unwinding the stack and cleaning up resources along the way.
+
+ Moreover, the code instructions written after `panic!` will no longer be executed.
+
+ Usually, `panics` can by avoided by pattern matching the error with `Result`.
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++println%21%28%22Reachable.%22%29%3B%0A%0A++++%2F%2F+Generates+a+panic%0A++++panic%21%28%22This+is+a+panic%21%22%29%3B%0A%0A++++%2F%2F+This+line+be+executed%0A++++println%21%28%22Unreachable%22%29%3B%0A%7D%0A
- title: Vectors
content_markdown: >
Some of the most useful generic types are collection types. A vector is a
@@ -209,6 +247,28 @@
reallocates its data on the heap to have a new fixed list with large capacity.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20%2F%2F%20We%20can%20be%20explicit%20with%20type%0A%20%20%20%20let%20mut%20i32_vec%20%3D%20Vec%3A%3A%3Ci32%3E%3A%3Anew()%3B%20%2F%2F%20turbofish%20%3C3%0A%20%20%20%20i32_vec.push(1)%3B%0A%20%20%20%20i32_vec.push(2)%3B%0A%20%20%20%20i32_vec.push(3)%3B%0A%0A%20%20%20%20%2F%2F%20But%20look%20how%20clever%20Rust%20is%20about%20determining%20the%20type%20automatically%0A%20%20%20%20let%20mut%20float_vec%20%3D%20Vec%3A%3Anew()%3B%0A%20%20%20%20float_vec.push(1.3)%3B%0A%20%20%20%20float_vec.push(2.3)%3B%0A%20%20%20%20float_vec.push(3.4)%3B%0A%0A%20%20%20%20%2F%2F%20That's%20a%20beautiful%20macro!%0A%20%20%20%20let%20string_vec%20%3D%20vec!%5BString%3A%3Afrom(%22Hello%22)%2C%20String%3A%3Afrom(%22World%22)%5D%3B%0A%0A%20%20%20%20for%20word%20in%20string_vec.iter()%20%7B%0A%20%20%20%20%20%20%20%20println!(%22%7B%7D%22%2C%20word)%3B%0A%20%20%20%20%7D%0A%7D%0A
+- title: Iterators
+ content_markdown: >
+ In Rust, an iterator is an abstraction that decomposed a collection into a sequence of values.
+
+ You've seen an iterator before: in the `for` loop
+ ```rust
+ // 0..5 iterates the values from 0 to 4
+ for i in 0..5 {
+ println!("{}", x);
+ }
+ ```
+
+ If you are to modify each element of the collection,
+ iterators were designed with a very powerful function, `.map()`.
+
+ `.map()` and `.iter()` are methods provided by iterators and are commonly
+ used in working with collections (vectors and String, for instance).
+
+ The function `.iter()` iterates over a reference to each element of a collection,
+ generating a sequence of elements.
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn+main%28%29+%7B%0A++++let+numbers+%3D+vec%21%5B1%2C+2%2C+3%2C+4%2C+5%5D%3B%0A%0A++++%2F%2F+each+element+will+be+modified%0A++++let+doubled%3A+Vec%3Ci32%3E+%3D+numbers.into_iter%28%29.map%28%7Cx%7C+x+*+2%29.collect%28%29%3B%0A++++%0A++++%0A++++%2F%2F+generating+a+sequence+with+all+elements%0A++++for+%26el+in+doubled.iter%28%29+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+el%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%7D%0A
- title: Chapter 4 - Conclusion
content_markdown: >
In one chapter we've learned how much power generic types give us! Don't
diff --git a/lessons/en/chapter_7.yaml b/lessons/en/chapter_7.yaml
index 430bc5c2a..c54a025f0 100644
--- a/lessons/en/chapter_7.yaml
+++ b/lessons/en/chapter_7.yaml
@@ -38,6 +38,39 @@
That said, Rust implements many programming language features, so that you
might not mind this lacking.
+- title: self and Self
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=struct+Coordinates+%7B%0A++++x%3A+i32%2C%0A++++y%3A+i32%2C%0A%7D%0A%0Aimpl+Coordinates+%7B%0A++++%2F%2F+the+return+value%2C+Self%2C+has+the+data+type+as+the+implemented+struct+%0A++++%2F%2F+this+method+is+static%0A++++fn+new%28xval%3A+i32%2C+yval%3A+i32%29+-%3E+Self+%7B%0A++++++++return+Coordinates+%7B%0A++++++++++++x%3A+xval%2C%0A++++++++++++y%3A+yval%2C%0A++++++++%7D%0A++++%7D%0A++++%0A++++fn+setx%28%26mut+self%2C+xval%3A+i32%29+%7B%0A++++++++self.x+%3D+xval%3B%0A++++%7D%0A++++%0A++++fn+sety%28%26mut+self%2C+yval%3A+i32%29+%7B%0A++++++++self.y+%3D+yval%3B%0A++++%7D%0A++++%0A++++fn+disp%28%26self%29+%7B%0A++++++++println%21%28%22Point+located+at+%3A+%7B%7D+on+OX++%7B%7D+on+OY%22%2C%0A++++++++++++++++self.x%2C+self.y%29%3B%0A++++%7D%0A++++%0A%7D%0A%0Afn+main%28%29+%7B%0A++++let+mut+p+%3D+Coordinates%3A%3Anew%28-1i32%2C+1i32%29%3B%0A++++p.disp%28%29%3B%0A++++p.setx%2810++as+i32%29%3B%0A++++p.sety%28-10+as+i32%29%3B%0A++++p.disp%28%29%3B%0A%7D%0A
+ content_markdown: >
+ In Rust, `self` and `Self` are fundamental concepts in ownership and
+ borrowing system. Both of them are used in `impl` and `trait` blocks.
+
+ `self`:
+ - refers to an **instance of struct**
+ - reference to itself
+ - is the first parameter of the method
+ - methods containing the `self` parameter are called using the name of the instance, the operator `.` and the name of the method
+
+
+ `Self`:
+ - refers to **return type** of the method
+ - has the same data type as the `struct` that is implemented in a `impl` block
+ - can be used for static methods using the operator `::` (they do not depend on a instance of a struct or contain `self` as a parameter)
+- title: Defining a macro
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=struct+Coordinates+%7B%0A++++x%3A+i32%2C%0A++++y%3A+i32%2C%0A%7D%0A%0Aimpl+Coordinates+%7B%0A++++%2F%2F+original+copy+constructor%2C+a+static+method%0A++++fn+new%28xval%3A+i32%2C+yval%3A+i32%29+-%3E+Self+%7B%0A++++++++Coordinates+%7B+x%3A+xval%2C+y%3A+yval+%7D%0A++++%7D%0A%7D%0A%0Aimpl+Coordinates+%7B%0A++++fn+disp%28%26self%29+%7B%0A++++++++println%21%28%22Point+located+at%3A+%7B%7D+on+OX%2C+%7B%7D+on+OY%22%2C+self.x%2C+self.y%29%3B%0A++++%7D%0A%7D%0A%0A%2F%2F+the+coord%21+macro%0Amacro_rules%21+coord+%7B%0A++++%28%24x%3Aexpr%2C+%24y%3Aexpr%29+%3D%3E+%7B%0A++++++++Coordinates%3A%3Anew%28%24x%2C+%24y%29%0A++++%7D%3B%0A%7D%0A%0Afn+main%28%29+%7B%0A++++%2F%2F+Use+the+coord%21+macro+to+create+a+new+Coordinates+instance%0A++++let+p1%3A+Coordinates+%3D+coord%21%281%2C+2%29%3B%0A%0A++++%2F%2F+Alternatively%2C+you+can+use+the+coord%21+macro+with+square+brackets%0A++++let+p2+%3D+coord%21%5B2%2C+2%5D%3B%0A++++%0A++++let+p3+%3D+Coordinates+%7B%0A++++++++x%3A+10i32%2C%0A++++++++y%3A+11+as+i32%2C%0A++++%7D%3B%0A++++%0A++++let+p4+%3D+Coordinates%3A%3Anew%28-10%2C+11%29%3B%0A%0A++++p1.disp%28%29%3B%0A++++p2.disp%28%29%3B%0A++++p3.disp%28%29%3B%0A++++p4.disp%28%29%3B%0A%7D%0A
+ content_markdown: >
+ We talked before that we can use `macros` to simplify abstractions of our code.
+
+ Let us define a `macro` that has the functionality of a copy constructor
+ for a struct.
+
+ Notice that we group the macro's parameters using either `()` or `[]`.
+
+ The choice between using `()` and `[]` for `macro` invocations in Rust
+ often depends on the expected syntax and visual aesthetics. While `()` is more commonly associated with function calls and grouping expressions, `[]` is often associated with array indexing or indexing-like operations.
+
+
- title: Encapsulation With Methods
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=struct%20SeaCreature%20%7B%0A%20%20%20%20noise%3A%20String%2C%0A%7D%0A%0Aimpl%20SeaCreature%20%7B%0A%20%20%20%20fn%20get_sound(%26self)%20-%3E%20%26str%20%7B%0A%20%20%20%20%20%20%20%20%26self.noise%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20creature%20%3D%20SeaCreature%20%7B%0A%20%20%20%20%20%20%20%20noise%3A%20String%3A%3Afrom(%22blub%22)%2C%0A%20%20%20%20%7D%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20creature.get_sound())%3B%0A%7D%0A
diff --git a/lessons/en/chapter_8.yaml b/lessons/en/chapter_8.yaml
index 79290ad46..848cbe269 100644
--- a/lessons/en/chapter_8.yaml
+++ b/lessons/en/chapter_8.yaml
@@ -26,6 +26,55 @@
will validate the lifetime of references doesn't last longer than what
it refers to (otherwise we'd get an error when we used it!).
+- title: What is a pointer?
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++let+a%3A+i32+%3D+12i32%3B%0A++++let+b%3A+%26i32+%3D+%26a%3B+++%2F%2F+pointer+to+i32%0A++++let+c%3A+%26%26i32+%3D+%26b%3B++%2F%2F+pointer+to+pointer+to+i32%0A++++let+d%3A+%26%26%26i32+%3D+%26c%3B%09%2F%2F+pointer+to+pointer+to+pointer+to+i32%0A++++%0A++++println%21%28%22%7B%7D+%7B%7D+%7B%7D+%7B%7D%22%2C+a%2C+b%2C+c%2C+d%29%3B%0A++++println%21%28%22%7B%7D+%7B%7D+%7B%7D+%7B%7D%22%2C+a%2C+*b%2C+**c%2C+***d%29%3B%0A%7D%0A
+ content_markdown: >
+ A pointer is a variable that stores a memory address.
+
+ Each variable:
+ - has a name
+ - has a data type
+ - has an address (in the RAM memory)
+ - stores an value
+
+ Why do use data types?
+
+ By using data types, we basically tell the CPU how much memory to
+ allocate for the variables we declare. For instance, the compiler
+ allocates 8 bytes for each `i8` and `u8` variable. Without pointers, a variables stores a `value`.
+
+ You've seen `u16`, `f64`, `usize`. These are data types.
+ So are `&i32`, `&&i32` `&&&i32` and so on.
+
+ The same logic applies to pointers. A pointer is a data type itself and
+ when we use pointer, the variables stores the `address` of the variable
+ instead of `value`. For pointer, the stored `value` is an `address`.
+
+
+ | variable name | data type | address | stored value |
+ | ------------- | --------- | ------- | --------------- |
+ | a | i32 | 0x100 | 12 |
+ | b = &a | &i32 | 0x132 | 0x100 (addr) |
+ | c = &b | &&i32 | 0x2a1 | 0x132 (addr) |
+ | d = &c | &&&i32 | 0x712 | 0x2a1 (addr) |
+
+ > The CPU chooses the address of a variable at runtime
+ > Do not expect to have the same address at different executions
+
+ All variables `i32`, `&i32`, `&&i32`, `&&&i32` are 32-bit variable.
+
+ `i32` -> stores a concrete value
+ `&...&i32` -> stores an address
+
+
+ However, when you try to print references (like `&i32`, `&&i32`, `&&&i32`),
+ Rust automatically `dereferences` them when using the `{}` format specifier in a `println!` `macro`. This means that the `values` themselves are
+ printed, not their `addresses`.
+
+ [
+ This is for `C`.
+ For Rust, think that `int` is `u128`, and `*` is Rust's `&`.
- title: Raw Pointers
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20a%20%3D%2042%3B%0A%20%20%20%20let%20memory_location%20%3D%20%26a%20as%20*const%20i32%20as%20usize%3B%0A%20%20%20%20println!(%22Data%20is%20here%20%7B%7D%22%2C%20memory_location)%3B%0A%7D%0A
diff --git a/lessons/en/chapter_9.yaml b/lessons/en/chapter_9.yaml
index ee8cb9e16..f05e357db 100644
--- a/lessons/en/chapter_9.yaml
+++ b/lessons/en/chapter_9.yaml
@@ -2,6 +2,27 @@
content_markdown: >
So far all of our code examples have been a single file. Let's discuss how
our code can be better organized and shared by others!
+- title: Cargo
+ content_markdown: >
+ `Cargo` is the official Rust package management tool for `crates`.
+ It helps us organise code in more than one file.
+
+ Rust programs may contain a `binary crate` (an executable,
+ for instance `./main`) or a `library crate` (a collection of functions,
+ data types, structs, trait and so on). You've already used a library in
+ writing code, which is `std` (the standard Rust library).
+
+
+ Therefore, there are two types of crates in Rust:
+ - `binary crates` : contain a `main.rs` file
+ - `library crates` : contain a `lib.rs` file
+
+ A `crate` cannot have both `main.rs` and `lib.rs`.
+
+ 
+
+ Resources:
+ - [https://youtu.be/969j0qnJGi8](https://youtu.be/969j0qnJGi8)
- title: Modules
content_markdown: |
Every Rust program or library is a *crate*.
@@ -13,12 +34,114 @@
A module can hold global variables, functions, structs, traits or even other modules!
In Rust there is not a 1 to 1 mapping of files to the module tree hierarchy. We must build the module tree explicitly by hand in our code.
+
+ More information about how to split you code into different files / modules
+ can be found [here](https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/crates-and-modules.html).
- title: Writing a Program
content_markdown: |
A program has a root module in a file called `main.rs`.
+- title: Binary Crate
+ content_markdown: |
+ For a `binary crate`, `cargo` has the following terminal commands:
+
+ ```
+ $ cargo new my-first-binary-crates # creates a binary create
+ $ cd my-first-binary-crates/
+ $ cargo build # equivalent of `rustc main.rs`
+ $ cargo run # equivalent of `./main`
+ ```
+
+ If you inspect the file hierarchy system, you'll notice that `cargo`
+ automatically creates some files and folders, and also writes some code.
+ Cool!
+
+ A `binary crate` will depend on the `main.rs`.
+ By the way, do not try to create a `lib.rs` here.
+ Our friend `cargo` will be confused.
+
- title: Writing a Library
content_markdown: |
A library has a root module in a file called `lib.rs`.
+- title: Library Crate
+ content_markdown: |
+ `cargo` has the following terminal commands for a `library crate` :
+
+ ```
+ $ cargo new --lib my-first-library # creates a library crate
+ $ cd my-first-library/
+ $ cargo build # links the dependencies between files (modules)
+ $ cargo test # checks the unit tests
+ ```
+
+ Unlike other Rust executable files you've written before,
+ a `library` contains functions and data types that are meant to be `used`
+ in other programs, in order to simplify some abstractions.
+
+ Think about the C `#include `. It is a library: it offers access
+ to functions, variables and even more. That's what a library is meant for.
+- title: Testing My Library
+ content_markdown: >
+ Rust libraries have a `lib.rs`, where you can test functionality of your
+ library.
+
+ When creating one, `cargo` helps you by creating some code:
+ ```rust
+ pub fn add(left: usize, right: usize) -> usize {
+ left + right
+ }
+
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+
+ #[test]
+ fn it_works() {
+ let result = add(2, 2);
+ assert_eq!(result, 4);
+ }
+ }
+
+ ```
+
+
+ If you need more tests, create functions annotated with `#[test]`.
+ Each function should use the macro `assert_eq!` or `assert!`.
+ Use this macro only once, since it exits the function once it is executed.
+
+ `lib.rs`:
+
+ ```
+ pub fn add(left: usize, right: usize) -> usize {
+ left + right
+ }
+
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+
+ #[test]
+ fn it_works() {
+ let result = add(2, 2);
+ assert_eq!(result, 4);
+ }
+
+ #[test]
+ fn add_zero() {
+ // Test if adding 0 to any number returns the same number
+ let result = add(5, 0);
+ assert_eq!(result, 5);
+ assert_eq!(result, 10); // this line will not be executed :)
+ }
+ }
+
+ ```
+
+
+ Test everything you create.
+
+
+ You can find [here](https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/testing.html)
+ more about [testing](https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/testing.html).
- title: Referencing Other Modules and Crates
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=use%20std%3A%3Af64%3A%3Aconsts%3A%3API%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22Welcome%20to%20the%20playground!%22)%3B%0A%20%20%20%20println!(%22I%20would%20love%20a%20slice%20of%20%7B%7D!%22%2C%20PI)%3B%0A%7D%0A
@@ -110,8 +233,8 @@
- title: Internal Module Referencing
content_markdown: >
Rust has several keywords you can use in your `use` path to quickly get
- ahold of the module you want:
-
+ a hold of the module you want:
+
* `crate` - the root module of your crate
* `super` - the parent module of your current module
@@ -130,6 +253,26 @@
a crate accessible by marking them as `pub` in the *root module* of your
crate (`lib.rs` or `main.rs`).
+- title: use
+ content_markdown: >
+ The `use` keyword has the purpose of bringing symbols into scope.
+
+ It allows you to use functions, structs or traits from a `module`
+ without using the full path each time.
+
+ The Rust `use` is equivalent to Python's `import` if we are to make
+ a comparison between these two programming languages.
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=%2F%2F+Bringing+the+%60println%60+function+into+scope%0Ause+std%3A%3Aio%3A%3Aprintln%3B%0A%0Afn+main%28%29+%7B%0A++++println%21%28%22Hello%2C+World%21%22%29%3B%0A%7D%0A
+- title: extern crate
+ content_markdown: >
+ The `extern crate` directive was used in older versions of Rust (before the 2018 edition).
+
+ Now, it is no longer required to use `extern crate` in code.
+
+ Instead, we specify the dependence on external crates in the `Cargo.toml` file
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=extern+crate+rand%3B%0A%0Afn+main%28%29+%7B%0A++++let+random_number+%3D+rand%3A%3Arandom%3A%3A%3Cu32%3E%28%29%3B%0A++++println%21%28%22Random+number%3A+%7B%7D%22%2C+random_number%29%3B%0A%7D%0A
- title: Structure Visibility
content_markdown: >
Just like functions, structures can declare what they want exposed outside
diff --git a/lessons/ro/chapter_1.yaml b/lessons/ro/chapter_1.yaml
index 887ecaed0..1d5bfeb6c 100644
--- a/lessons/ro/chapter_1.yaml
+++ b/lessons/ro/chapter_1.yaml
@@ -13,6 +13,26 @@
Odată ce vă veți familiariza cu Rust, puteți să vă declarați un **Rustaceu**. Așa
se numesc toți oamenii care folosesc, contribuie sau sunt interesați de Rust.
+
+- title: Ce este Rust?
+ content_markdown: >
+ Rust este un limbaj de programare al sistemelor care este cunoscut pentru accentul pe care îl pune
+ pe siguranță, performanță și concurență.
+
+ Rust este conceput pentru a oferi control la un nivel aproape de hardware asupra resurselor sistemului
+ fără a sacrifica abstracţiile de nivel înalt.
+
+ Ce are Rust diferit de alte limbaje de programare.
+ - proprietate și împrumut
+ - durata de viață
+ - potrivire de model (pattern matching)
+ - concurență fără curse de date (rivalitate de date)
+ - trăsături (trait-uri)
+ - absența valorii `null` sau a colectării gunoiului (garbage collection)
+ - macro-uri
+ - pachete numite "cutii" (crates)
+ - comunitate și ecosistem
+ - UTF-8 și text Unicode
- title: Rust Playground
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20println!(%22Bun%20venit%20în%20locul%20de%20joacă!%20Puteți%20modifica%20codul%20aici.%22)%3B%0A%7D%0A
@@ -23,6 +43,37 @@
E o modalitate bună de a vă juca cu Rust și a arăta altora
creativitatea și provocările dumneavoastră!
+
+- title: Ce este println! ?
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++println%21%28%29%3B%0A++++print%21%28%22Este+o+linie+deasupra.+%22%29%3B%0A++++print%21%28%22Ce+tare%2C+nu%3F%22%29%3B%0A++++println%21%28%29%3B+++++%2F%2F+linie+nou%C4%83+la+finalul+consolei%0A%7D%0A
+ content_markdown: >
+ Spre deosebire de alte limbaje de programare, unde există o funcție pentru
+ afişarea la `stdout` (consolă), Rust folosește `macro-urile` **println!** și **print!**.
+ Vom vorbi despre `macro-uri` mai târziu.
+
+ > `println!` va afișa un nou caracter de linie nouă `\n` la sfârșitul șirului
+ > `print!` nu va folosi `\n` de la sfârșitul textului
+
+- title: Cum să compilezi?
+ content_markdown: >
+ Pe Linux / MacOS:
+ ```
+ $ rustc main.rs
+ $ ./main
+ ```
+
+ Pe sistemele Windows:
+ ```
+ > rustc main.rs
+ > .\main.exe
+ ```
+
+ | System de Operare (OS) | comenzi de rulat în terminal |
+ | ---------------------- | --------------------------------- |
+ | Linux / MacOS | $ rustc main.rs
$ ./main |
+ | Windows | > rustc main.rs
> ./main.exe |
+
- title: Variabile
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20%2F%2F%20Rust%20intuie%C8%99te%20tipul%20de%20date%20pentru%20x%0A%20%20%20%20let%20x%20%3D%2013%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%0A%20%20%20%20%2F%2F%20Rust%20poate%20fi%20%C8%99i%20explicit%20atunci%20c%C3%A2nd%20stabile%C8%99te%20tipul%20de%20date%0A%20%20%20%20let%20x%3A%20f64%20%3D%203.14159%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%0A%20%20%20%20%2F%2F%20Rust%20poate%20declara%20o%20variabil%C4%83%20%C8%99i%20s%C4%83%20o%20ini%C8%9Bializeze%20mai%20t%C3%A2rziu%2C%0A%20%20%20%20%2F%2F%20dar%20acest%20lucru%20nu%20se%20face%20des%0A%20%20%20%20let%20x%3B%0A%20%20%20%20x%20%3D%200%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%7D%0A
@@ -64,12 +115,34 @@
O să revenim pentru a vorbi mai multe despre acest concept, dar deocamdată doar fiți
atenți la acest cuvânt cheie.
+
- title: Tipuri de date de bază
code: >-
- https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn+main%28%29+%7B%0A++++let+x+%3D+12%3B+%2F%2F+acesta+este+un+i32+%C3%AEn+mod+implicit%0A++++let+a+%3D+12u8%3B%0A++++let+b+%3D+4.3%3B+%2F%2F+acesta+este+un+f64+%C3%AEn+mod+implicit%0A++++let+c+%3D+4.3f32%3B%0A++++let+d+%3D+%27r%27%3B+%2F%2F+caracter+unicode%0A++++let+ferris+%3D+%27%F0%9F%A6%80%27%3B+%2F%2F+tot+un+caracter+unicode%0A++++let+bv+%3D+true%3B%0A++++let+t+%3D+%2813%2C+false%29%3B%0A++++let+sentence+%3D+%22hello+world%21%22%3B%0A++++println%21%28%0A++++++++%22%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D+%7B%7D%22%2C%0A++++++++x%2C+a%2C+b%2C+c%2C+d%2C+ferris%2C+bv%2C+t.0%2C+t.1%2C+sentence%0A++++%29%3B%0A%7D%0A
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++let+a+%3D+13u8%3B%0A++++let+b+%3D+7u32%3B%0A++++let+c+%3D+a+as+u32+%2B+b%3B%0A++++println%21%28%22%7B%7D%22%2C+c%29%3B%0A%0A++++let+t+%3D+true%3B%0A++++println%21%28%22%7B%7D%22%2C+t+as+u8%29%3B%0A%0A++++%0A++++let+d%3A+u32+%3D+12%3B%0A++++%2F%2F+let+e%3A+u32+%3D+-12%3B++%2F%2F+numerele+%60unsigned%60+%28fara+semn%29+includ+doar+nunmerele+pozitive%0A++++let+e%3A+i32+%3D+12%3B%0A++++let+f%3A+i32+%3D+-12%3B%0A++++println%21%28%22%7B%7D+%7B%7D+%7B%7D+%7B%7D%22%2C%0A++++++++++++++++d%2C+-%28d+as+i32%29%2C+e%2C+f%29%3B%0A%7D%0A%0A
content_markdown: >
- Rust are o varietate de tipuri de date familiare dumneavoastră:
+ Tipuri de date de bază
+ - bool
+ - u8
+ - u16
+ - u32
+ - u64
+ - u128
+ - i8
+ - i16
+ - i32
+ - i64
+ - i128
+ - usize
+ - isize
+ - f32
+ - f64
+
+ | u/i | semn | sign |semn |
+ | --- | ---------------- | ------------------------------------ | --- |
+ | `u` | numere fără semn | doar numere pozitive | + |
+ | `i` | numere cu semn | atâ numere pozitive, cât și negative | ± |
+ Rust are o varietate de tipuri de date familiare dumneavoastră:
* variabilă booleană - `bool` pentru a reprezenta adevărat și fals
@@ -103,6 +176,9 @@
Tipurile numerice pot fi specificate explicit prin adăugarea tipului la finalul numărului (ex: `13u32`, `2u8`).
+
+ > nu se poate aplica operatorul unar `-` pe `u8`, `u17`, `u64`, `u128` și `usize`
+
- title: Conversia tipurilor de bază
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20a%20%3D%2013u8%3B%0A%20%20%20%20let%20b%20%3D%207u32%3B%0A%20%20%20%20let%20c%20%3D%20a%20as%20u32%20%2B%20b%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20c)%3B%0A%0A%20%20%20%20let%20t%20%3D%20true%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20t%20as%20u8)%3B%0A%7D%0A
@@ -130,7 +206,49 @@
Spre deosebire de variabile, constantelor trebuie să li se specifice explicit tipul la declarare.
- Numele constantelor sunt mereu scrise în format `SCREAMING_SNAKE_CASE` (ex: `MY_FIRST_CONSTANT`).
+ Numele constantelor sunt mereu scrise în format `SCREAMING_SNAKE_CASE` (ex: MY\_FIRST\_CONSTANT).
+
+- title: Colecții
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=use+std%3A%3Acollections%3A%3AHashMap%3B%0Ause+std%3A%3Acollections%3A%3AHashSet%3B%0A%0Afn+main%28%29+%7B%0A%0A++++%2F%2F+array%0A++++let+array%3A+%5Bi32%3B+3%5D+%3D+%5B1%2C+2%2C+3%5D%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+array%29%3B%0A++++%0A++++%2F%2F+vector%0A++++let+vector%3A+Vec%3Ci32%3E+%3D+vec%21%5B1%2C+2%2C+3%5D%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+vector%29%3B%0A%0A++++%2F%2F+slice+%28from+other+collection%29%0A++++let+slice+%3D+%26array%5B1..3%5D%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+slice%29%3B%0A++++%0A++++%2F%2F+string%0A++++let+string%3A+String+%3D+String%3A%3Afrom%28%22Hello%2C+Rust%21%22%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+string%29%3B%0A++++%0A++++%2F%2F+tuple%0A++++let+tuple%3A+%28i32%2C+f64%2C+u8%29+%3D+%2842%2C+3.14%2C+5%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+tuple%29%3B%0A%0A++++%2F%2F+map%0A++++let+mut+map+%3D+HashMap%3A%3Anew%28%29%3B%0A++++map.insert%28%22one%22%2C+1%29%3B%0A++++map.insert%28%22two%22%2C+2%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+map%29%3B%0A++++%0A++++%2F%2F+set%0A++++let+mut+set+%3D+HashSet%3A%3Anew%28%29%3B%0A++++set.insert%281%29%3B%0A++++set.insert%282%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+set%29%3B%0A%7D%0A
+ content_markdown: >
+ Rust oferă o varietate de tipuri de colecție care vă permit să stocați și
+ manipula datele.
+
+ Principalele colecții din Rust:
+ - Matrice
+ - Vectori
+ - Felii
+ - Siruri de caractere
+ - Tuplu
+ - Hărți hash
+ - Seturi de hash
+
+ Vom discuta pe fiecare dintre ele mai târziu.
+
+- title: Afişarea cu `{:?}`
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A%0A++++let+my_vector+%3D+vec%21%5B1%2C+2%2C+3%5D%3B%0A++++%2F%2F+println%21%28%22%7B%7D%22%2C+my_vector%29%3B+++%2F%2F+will+generate+an+error%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+my_vector%29%3B%0A++++println%21%28%22%7B%3A%23%3F%7D%22%2C+my_vector%29%3B+++%2F%2F+debug+mode%0A++++%0A++++let+x%3A+i32+%3D+-12%3B%0A++++println%21%28%22%7B%7D%22%2C+x%29%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+x%29%3B%0A++++println%21%28%22%7B%3A%23%3F%7D%22%2C+x%29%3B%09%0A%7D%0A
+ content_markdown: >
+ Observați că atunci când afișați o colecție, `println!("{}", collection);` nu merge.
+
+ În Rust, `:?` este un specificator de format folosit cu trăsătura `Debug` when
+ imprimarea valorilor / colecţiilor de valori.
+
+ Când utilizați `println!("{:?}", x);`, compilatorul Rust formatează variabila
+ în consecinţă.
+
+ Acest lucru este util în special pentru „colecții” precum vectori, matrice, structuri
+ si enumerari.
+
+ `{:#?}` le va tipări într-un mod care este conceput pentru a fi informativ în timpul
+ depanare.
+
+ | Printing | tip de bază | colecție |
+ | --------------------- | ----------- | -------- |
+ | println!("{}", x); | ✅ | ❌ |
+ | println!("{:?}", x); | ✅ | ✅ |
+ | println!("{:#?}", x); | ✅ | ✅ |
- title: Tablouri
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20nums%3A%20%5Bi32%3B%203%5D%20%3D%20%5B1%2C%202%2C%203%5D%3B%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20nums)%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20nums%5B1%5D)%3B%0A%7D%0A
@@ -149,6 +267,22 @@
Colecțiile cu dimensiune dinamică, deseori numite tablouri dinamice sau variabile, vă
vor fi prezentate într-un capitol viitor numit **Vectori**.
+
+
+- title: Vectori
+ code: >-
+ https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++%2F%2F+vec%21%5B%5D+este+un+macor%0A++++let+v1+%3D+vec%21%5B1%2C+2%2C+3%2C+4%2C+5%5D%3B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+v1%29%3B%0A++++%0A++++let+mut+v2+%3D+Vec%3A%3Anew%28%29%3B%0A++++v2.push%281%29%3B%0A++++v2.push%282%29%3B%0A++++v2.push%283%29%3B%0A++++%0A++++%2F%2F+folosim+o+bucla+%60for%60%0A++++for+el+in+%26v1+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+el%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A++++%0A++++%2F%2F+folosim+metoda+%60.iter%28%29%60%0A++++for+el+in+v1.iter%28%29+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+el%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A++++%0A++++for+i+in+0..v1.len%28%29+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+v1%5Bi%5D%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A++++%0A++++%0A++++%2F%2F+vom+vorbi+mai+tarziu+despre+Some+%C5%9Fi+Result%0A++++if+let+Some%28element%29+%3D+v1.get%281%29+%7B%0A++++++++println%21%28%22Second+element%3A+%7B%7D%22%2C+element%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22Index+out+of+bounds.%22%29%3B%0A++++%7D%0A++++%0A%7D%0A
+ content_markdown: >
+ Vectorii sunt unul dintre cele mai flexibile și mai frecvent utilizate tipuri de colecție în Rust.
+
+ Ele reprezintă un tablou dinamic, care poate fi extins și fac parte din
+ biblioteca standard Rust (`std::vec::Vec`). Nu este nevoie să-l „utilizați”. :)
+
+- ttile: Strings
+ code: >-
+ content_markdown: >
+
+
- title: Funcții
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20add(x%3A%20i32%2C%20y%3A%20i32)%20-%3E%20i32%20%7B%0A%20%20%20%20return%20x%20%2B%20y%3B%0A%7D%0A%0Afn%20subtract(x%3A%20i32%2C%20y%3A%20i32)%20-%3E%20i32%20%7B%0A%20%20%20%20x%20-%20y%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20println!(%2242%20%2B%2013%20%3D%20%7B%7D%22%2C%20add(42%2C%2013))%3B%0A%20%20%20%20println!(%2242%20-%2013%20%3D%20%7B%7D%22%2C%20subtract(42%2C%2013))%3B%0A%7D%0A