forked from UPB-CS-OpenSourceUpstream/tour_of_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_2.yaml
150 lines (105 loc) · 9.94 KB
/
chapter_2.yaml
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
56
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
- title: Chapter 2 - Basic Control Flow
content_markdown: >
In this chapter let's talk about basic control flow methods in Rust.
If you are familiar with C based languages you'll feel right at home and
maybe
enjoy a surprise or two.
- title: if/else
content_markdown: >
Code branching in Rust is not surprising.
Conditions don't have parentheses! Did we ever really need them? Our logic
now looks nice and clean.
All your usual relational and logical operators still work: `==`, `!=`, `<`,
`>`, `<=`, `>=`, `!`, `||`, `&&`.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%2042%3B%0A%20%20%20%20if%20x%20%3C%2042%20%7B%0A%20%20%20%20%20%20%20%20println!(%22less%20than%2042%22)%3B%0A%20%20%20%20%7D%20else%20if%20x%20%3D%3D%2042%20%7B%0A%20%20%20%20%20%20%20%20println!(%22is%2042%22)%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20println!(%22greater%20than%2042%22)%3B%0A%20%20%20%20%7D%0A%7D
- title: loop
content_markdown: |
Need an infinite loop?
Rust makes it easy.
`break` will escape a loop when you are ready.
`loop` has a secret we'll talk about soon.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20mut%20x%20%3D%200%3B%0A%20%20%20%20loop%20%7B%0A%20%20%20%20%20%20%20%20x%20%2B%3D%201%3B%0A%20%20%20%20%20%20%20%20if%20x%20%3D%3D%2042%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%7D%0A
- title: while
content_markdown: |
`while` lets you easily add a condition to a loop.
If the condition evaluates to `false`, the loop will exit.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20mut%20x%20%3D%200%3B%0A%20%20%20%20while%20x%20!%3D%2042%20%7B%0A%20%20%20%20%20%20%20%20x%20%2B%3D%201%3B%0A%20%20%20%20%7D%0A%20%20%20%20println!(%22x%20is%20%7B%7D%22%2C%20x)%3B%0A%7D%0A
- title: for
content_markdown: >
Rust's `for` loop is a powerful upgrade. It
iterates over values from any expression that evaluates into an iterator.
What's
an iterator? An iterator is an object that you can ask the question "What's
the
next item you have?" until there are no more items.
We'll explore this more in a future chapter. In the meantime, just know Rust
makes
it easy to create iterators that generate a sequence of integer numbers.
The `..` operator creates an iterator that generates numbers from a start
number up to
but not including an end number.
The `..=` operator creates an iterator that generates numbers from a start
number up to
and including an end number.
code: >-
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 for
matching all possible conditions of a value
and executing a code path if the match is true.
Let's see how this works for numbers. We will have more to
say in future chapters on pattern matching more complex data. I promise you
it will be worth the wait.
`match` is exhaustive so all cases must be handled.
If the branches do not cover all the possible cases,
we'll use the underscore `_`, that will take place for the rest of them,
Matching combined with destructuring is by far one of the most common patterns you will see in all of Rust.
Matching combined with destructuring is by far one of the
most common patterns you will see in all of Rust.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn+main%28%29+%7B%0A++++let+x+%3D+42%3B%0A%0A++++match+x+%25+2+%3D%3D+0+%7B%0A++++++++true+%3D%3E+println%21%28%22%7B%7D+is+an+even+number%22%2C+x%29%2C%0A++++++++false+%3D%3E+println%21%28%22%7B%7D+is+an+odd+number%22%2C+x%29%2C%0A++++%7D%0A%0A++++match+x+%7B%0A++++++++0+%3D%3E+%7B%0A++++++++++++println%21%28%22found+zero%22%29%3B%0A++++++++%7D%0A++++++++%2F%2F+we+can+match+against+multiple+values%0A++++++++1+%7C+2+%3D%3E+%7B%0A++++++++++++println%21%28%22found+1+or+2%21%22%29%3B%0A++++++++%7D%0A++++++++%2F%2F+we+can+match+against+ranges%0A++++++++3..%3D9+%3D%3E+%7B%0A++++++++++++println%21%28%22found+a+number+3+to+9+inclusively%22%29%3B%0A++++++++%7D%0A++++++++%2F%2F+we+can+bind+the+matched+number+to+a+variable%0A++++++++matched_num+%40+10..%3D100+%3D%3E+%7B%0A++++++++++++println%21%28%22found+%7B%7D+number+between+10+to+100%21%22%2C+matched_num%29%3B%0A++++++++%7D%0A++++++++%2F%2F+do+nothing+if+x+equals+101%0A++++++++101+%3D%3E+%28%29%2C%0A++++++++%2F%2F+this+is+the+default+match+that+must+exist+if+not+all+cases+are+handled%0A++++++++_+%3D%3E+%7B%0A++++++++++++println%21%28%22found+something+else%21%22%29%3B%0A++++++++%7D%0A++++%7D%0A%7D%0A
- title: matching multiple parameters
content_markdown: >
The `match` statements expects a tuple.
Notice that, unlike the `if` statement,
the code will compile just fine if you try
to place the evaluation elements in parenthesis `()`.
But why?...🤔
Because the element is seen as a `tuple`
(a collection of elements that are able to have different types).
`tuples` are really powerful to use if you want
to match multiple elements together.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn+main%28%29+%7B%0A++++let+x+%3D+42%3B%0A++++let+y+%3D+-101%3B%0A+%0A++++%2F%2F+the+program+compiles%0A++++%2F%2F+but+the+parantheses+are+unnecessary+for+just+an+element%0A++++match+%28x+%25+2+%3D%3D+0%29+%7B%0A++++++++%28true%29+%3D%3E+println%21%28%22%7B%7D+is+an+even+number%22%2C+x%29%2C%0A++++++++false+%3D%3E+println%21%28%22%7B%7D+is+an+odd+number%22%2C+x%29%2C%0A++++%7D%0A%0A+%0A++++match+%28x+%3E%3D+0%2C+y+%3E%3D+0%29+%7B%0A++++++++%28true%2C+true%29+%3D%3E+println%21%28%22Both+numbers+%7B%7D+%7B%7D+are+positive%22%2C+x%2C+y%29%2C%0A++++++++%28true%2C+false%29+%3D%3E+println%21%28%22Only+%7B%7D+is+a+positive+number%22%2C+x%29%2C%0A++++++++%28false%2C+true%29+%3D%3E+println%21%28%22Only+%7B%7D+is+a+negative+number%22%2C+y%29%2C%0A++++++++%28false%2C+false%29+%3D%3E+println%21%28%22Both+numbers+%7B%7D+%7B%7D+are+negative%22%2C+x+%2Cy%29%2C%0A++++%7D%0A%7D%0A
- title: Returning Values From loop
content_markdown: |
`loop` can break to return a value.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20mut%20x%20%3D%200%3B%0A%20%20%20%20let%20v%20%3D%20loop%20%7B%0A%20%20%20%20%20%20%20%20x%20%2B%3D%201%3B%0A%20%20%20%20%20%20%20%20if%20x%20%3D%3D%2013%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%20%22found%20the%2013%22%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%3B%0A%20%20%20%20println!(%22from%20loop%3A%20%7B%7D%22%2C%20v)%3B%0A%7D%0A
- title: Returning Values From Block Expressions
content_markdown: >
`if`, `match`, functions, and scope blocks all have a unique way of
returning values in Rust.
If the last statement in an `if`, `match`, function, or scope block is an
expression without
a `;`, Rust will return it as a value from the block. This
is a great way to create concise logic that returns a value that can be put
into
a new variable.
Notice that it also allows an `if` statement to operate like a concise
ternary expression.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20example()%20-%3E%20i32%20%7B%0A%20%20%20%20let%20x%20%3D%2042%3B%0A%20%20%20%20%2F%2F%20Rust's%20ternary%20expression%0A%20%20%20%20let%20v%20%3D%20if%20x%20%3C%2042%20%7B%20-1%20%7D%20else%20%7B%201%20%7D%3B%0A%20%20%20%20println!(%22from%20if%3A%20%7B%7D%22%2C%20v)%3B%0A%0A%20%20%20%20let%20food%20%3D%20%22hamburger%22%3B%0A%20%20%20%20let%20result%20%3D%20match%20food%20%7B%0A%20%20%20%20%20%20%20%20%22hotdog%22%20%3D%3E%20%22is%20hotdog%22%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20notice%20the%20braces%20are%20optional%20when%20its%20just%20a%20single%20return%20expression%0A%20%20%20%20%20%20%20%20_%20%3D%3E%20%22is%20not%20hotdog%22%2C%0A%20%20%20%20%7D%3B%0A%20%20%20%20println!(%22identifying%20food%3A%20%7B%7D%22%2C%20result)%3B%0A%0A%20%20%20%20let%20v%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20This%20scope%20block%20lets%20us%20get%20a%20result%20without%20polluting%20function%20scope%0A%20%20%20%20%20%20%20%20let%20a%20%3D%201%3B%0A%20%20%20%20%20%20%20%20let%20b%20%3D%202%3B%0A%20%20%20%20%20%20%20%20a%20%2B%20b%0A%20%20%20%20%7D%3B%0A%20%20%20%20println!(%22from%20block%3A%20%7B%7D%22%2C%20v)%3B%0A%0A%20%20%20%20%2F%2F%20The%20idiomatic%20way%20to%20return%20a%20value%20in%20rust%20from%20a%20function%20at%20the%20end%0A%20%20%20%20v%20%2B%204%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22from%20function%3A%20%7B%7D%22%2C%20example())%3B%0A%7D%0A
- title: Chapter 2 - Conclusion
content_markdown: >
Hopefully I've shown a glimpse of Rust's power even in the most
basic language features. We'll be talking about `for` and
`match` even more in depth as we gain more knowledge that
can utilize their capabilities. Next time we'll get into Rust's foundational
data
structures.