Skip to content

Commit 234d72c

Browse files
committed
Run prettier
1 parent 173dc5a commit 234d72c

File tree

31 files changed

+1064
-519
lines changed

31 files changed

+1064
-519
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Rust
22

3-
This contains the files for the rust course, available at https://cratecode.com/unit/xa3l5ahj5w. Learn more about creating your own lessons with the [CLI](https://github.com/Cratecode/cli) and [upload action](https://github.com/Cratecode/upload-unit).
3+
This contains the files for the rust course, available at
4+
https://cratecode.com/unit/xa3l5ahj5w. Learn more about creating your own
5+
lessons with the [CLI](https://github.com/Cratecode/cli) and
6+
[upload action](https://github.com/Cratecode/upload-unit).

sections/00_rust_concepts/00_rust_intro/README.md

Lines changed: 163 additions & 23 deletions
Large diffs are not rendered by default.

sections/00_rust_concepts/00_rust_intro/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"defaultFile": "src/main.rs",
33
"source": "https://github.com/Cratecode/rust/tree/master/sections/00_rust_concepts/00_rust_intro",
44
"showShareButton": true
5-
}
5+
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"type": "lesson",
3-
"id": "les_rust_intro",
4-
"extends": "basic",
5-
"name": "Rust - A Language You'll Love",
6-
"unit" : "rust_intro",
7-
"spec": "A Mandelbrot Set renderer written in Rust which creates a plot on the console.",
8-
"class": "tutorial"
2+
"type": "lesson",
3+
"id": "les_rust_intro",
4+
"extends": "basic",
5+
"name": "Rust - A Language You'll Love",
6+
"unit": "rust_intro",
7+
"spec": "A Mandelbrot Set renderer written in Rust which creates a plot on the console.",
8+
"class": "tutorial"
99
}
Lines changed: 72 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Rust Ownership and Borrowing
22

3-
The way Rust treats variables is fundamentally different from how they're handled in many other languages.
4-
Take a look at this JavaScript code:
3+
The way Rust treats variables is fundamentally different from how they're
4+
handled in many other languages. Take a look at this JavaScript code:
5+
56
```js
67
const arr = [1, 2, 3];
78
const arr2 = arr;
@@ -10,41 +11,46 @@ arr[0] = 1000;
1011
console.log(arr, arr2); // [1000, 2, 3] [1000, 2, 3]
1112
```
1213

13-
In many languages, variables act like names to assign to pieces of data.
14-
In this example, `arr` and `arr2` are both aliases that really mean the same list.
15-
This is not how variables work in Rust.
14+
In many languages, variables act like names to assign to pieces of data. In this
15+
example, `arr` and `arr2` are both aliases that really mean the same list. This
16+
is not how variables work in Rust.
1617

1718
## Ownership
1819

19-
Rust variables have the concept of "ownership".
20-
This basically means that a variable owns a piece of data.
21-
Another way to think about is it that a piece of data actually lives inside the variable itself.
22-
This is a fundamental concept in Rust, so it's important to understand it.
23-
If we wanted to write the same piece of code in Rust, it wouldn't do the same thing. If we have something like this:
20+
Rust variables have the concept of "ownership". This basically means that a
21+
variable owns a piece of data. Another way to think about is it that a piece of
22+
data actually lives inside the variable itself. This is a fundamental concept in
23+
Rust, so it's important to understand it. If we wanted to write the same piece
24+
of code in Rust, it wouldn't do the same thing. If we have something like this:
25+
2426
```rust
2527
let arr = vec![1, 2, 3];
2628
let arr2 = arr;
2729
```
2830

29-
Then the data (a Vec, which is basically the same thing as an array in JavaScript and a list in other languages) is first being placed inside of `arr`, then being **moved** from `arr` to `arr2`.
30-
After it's moved, there isn't anything in `arr`, so we can't use it.
31+
Then the data (a Vec, which is basically the same thing as an array in
32+
JavaScript and a list in other languages) is first being placed inside of `arr`,
33+
then being **moved** from `arr` to `arr2`. After it's moved, there isn't
34+
anything in `arr`, so we can't use it.
3135

32-
This is the core idea behind how variables work in Rust.
33-
Instead of thinking of variables as a way to name data, think of them as the place where the data is actually stored.
34-
If the variable no longer exists (i.e., goes out of scope), then neither does the data.
36+
This is the core idea behind how variables work in Rust. Instead of thinking of
37+
variables as a way to name data, think of them as the place where the data is
38+
actually stored. If the variable no longer exists (i.e., goes out of scope),
39+
then neither does the data.
3540

3641
## Borrowing
3742

38-
Of course, there's still a way to emulate the JavaScript code above.
39-
We can create these sorts of "aliases" by using **references** to our variables.
40-
These are also called **borrows** (you can think of it as borrowing the data from a variable).
41-
It's important to consider that,
42-
just like how Rust variables represent where the data "lives", references point to variables, not the data inside them.
43-
If we try to move data while a reference to it exists,
44-
we'll get compiler errors because Rust doesn't let us have a reference to something that doesn't exist
45-
(check out the code for an example).
43+
Of course, there's still a way to emulate the JavaScript code above. We can
44+
create these sorts of "aliases" by using **references** to our variables. These
45+
are also called **borrows** (you can think of it as borrowing the data from a
46+
variable). It's important to consider that, just like how Rust variables
47+
represent where the data "lives", references point to variables, not the data
48+
inside them. If we try to move data while a reference to it exists, we'll get
49+
compiler errors because Rust doesn't let us have a reference to something that
50+
doesn't exist (check out the code for an example).
4651

4752
Now, let's try writing that code in Rust:
53+
4854
```rust
4955
// We need to mark variables as mutable so that we can modify them.
5056
let mut arr = vec![1, 2, 3];
@@ -68,16 +74,17 @@ let arr2 = &arr;
6874
let mut arr3 = &arr;
6975
```
7076

71-
But that's about as far as we can go.
72-
One of Rust's rules with borrows is that we can't modify data if an immutable reference to it exists.
73-
Another big rule is that we can have one mutable reference or as many immutable references as we want, but never both.
74-
So, we're stuck.
75-
This is one of the things that creates the most frustration when working in Rust.
76-
There's always a way forward, but it either requires thinking of our problem differently,
77-
or using other tools that Rust provides us.
78-
In most cases, we'll have to take the first option, and this case is no exception.
77+
But that's about as far as we can go. One of Rust's rules with borrows is that
78+
we can't modify data if an immutable reference to it exists. Another big rule is
79+
that we can have one mutable reference or as many immutable references as we
80+
want, but never both. So, we're stuck. This is one of the things that creates
81+
the most frustration when working in Rust. There's always a way forward, but it
82+
either requires thinking of our problem differently, or using other tools that
83+
Rust provides us. In most cases, we'll have to take the first option, and this
84+
case is no exception.
7985

8086
If we wanted to get the same result, we could simply use:
87+
8188
```rust
8289
let mut arr = vec![1, 2, 3];
8390

@@ -87,10 +94,10 @@ arr[0] = 1000;
8794
println!("{arr:?} {arr:?}");
8895
```
8996

90-
References are extremely useful when dealing with functions.
91-
They let us pass our data to a function without actually giving the data to the function
92-
(so the function "borrows" it instead).
93-
For example:
97+
References are extremely useful when dealing with functions. They let us pass
98+
our data to a function without actually giving the data to the function (so the
99+
function "borrows" it instead). For example:
100+
94101
```rust
95102
// This will move `data` into `my_function_1`.
96103
my_function_1(data);
@@ -112,20 +119,23 @@ my_function_2(&data);
112119

113120
### Mutable References
114121

115-
Mutable references are similar to normal references, but they let us modify the data that they're borrowing.
116-
There can only ever be one mutable reference, and it can't exist if there are immutable references.
122+
Mutable references are similar to normal references, but they let us modify the
123+
data that they're borrowing. There can only ever be one mutable reference, and
124+
it can't exist if there are immutable references.
117125

118-
In general, this is fine.
119-
There aren't that many cases where you'd need to give away two mutable references simultaneously.
120-
Computers run programs sequentially (one step after another), so you can just create mutable references as needed.
121-
In fact, the place where this falls apart is with concurrent programming, where steps aren't executed one after another.
122-
There are more language features that can be used to circumvent that, but we'll talk about those later.
126+
In general, this is fine. There aren't that many cases where you'd need to give
127+
away two mutable references simultaneously. Computers run programs sequentially
128+
(one step after another), so you can just create mutable references as needed.
129+
In fact, the place where this falls apart is with concurrent programming, where
130+
steps aren't executed one after another. There are more language features that
131+
can be used to circumvent that, but we'll talk about those later.
123132

124133
## Lifetimes
125134

126-
We'll go into more depth on lifetimes in the next lesson,
127-
but the big idea behind them is that data only lives for a certain amount of time on the computer.
128-
Take a look at this code, for example:
135+
We'll go into more depth on lifetimes in the next lesson, but the big idea
136+
behind them is that data only lives for a certain amount of time on the
137+
computer. Take a look at this code, for example:
138+
129139
```rust
130140
// This creates an uninitialized variable.
131141
// Rust only allows this to exist if it's guaranteed that
@@ -140,22 +150,24 @@ let list_ref;
140150
// can't be accessed anymore, and so are removed from memory.
141151
{
142152
let list = vec![1, 2, 3];
143-
153+
144154
list_ref = &list;
145155
}
146156

147157
println!("{list_ref:?}");
148158
```
149159

150-
This code will not work.
151-
The issue is that we're setting `list_ref` to a reference of `list`, but `list` only exists within that block.
152-
As soon as it isn't accessible anymore, it'll get removed from memory,
153-
so the `println` wouldn't have anything to access.
160+
This code will not work. The issue is that we're setting `list_ref` to a
161+
reference of `list`, but `list` only exists within that block. As soon as it
162+
isn't accessible anymore, it'll get removed from memory, so the `println`
163+
wouldn't have anything to access.
154164

155165
In some languages, doing something like this might lead to undefined behavior.
156-
Our reference would still point to a place in memory (specifically, the point that the Vec used to take up),
157-
but that memory could contain anything at all when we print it.
158-
Because this leads to undefined behavior, Rust gives us an error:
166+
Our reference would still point to a place in memory (specifically, the point
167+
that the Vec used to take up), but that memory could contain anything at all
168+
when we print it. Because this leads to undefined behavior, Rust gives us an
169+
error:
170+
159171
```rust
160172
error[E0597]: `list` does not live long enough
161173
--> src/main.rs:16:16
@@ -169,19 +181,18 @@ error[E0597]: `list` does not live long enough
169181
| -------- borrow later used here
170182
```
171183

172-
This is the basic idea behind lifetimes.
173-
Data only lives for a certain amount of time,
174-
and if references outlive the things they point to, issues start to creep up.
175-
To fix this, we would need to make `list` live longer.
176-
An easy way to do that is to move it into a variable that lives longer:
184+
This is the basic idea behind lifetimes. Data only lives for a certain amount of
185+
time, and if references outlive the things they point to, issues start to creep
186+
up. To fix this, we would need to make `list` live longer. An easy way to do
187+
that is to move it into a variable that lives longer:
177188

178189
```rust
179190
let new_list;
180191
let list_ref;
181192

182193
{
183194
let list = vec![1, 2, 3];
184-
195+
185196
// It needs to be done in this order because
186197
// of our rules with moving while we have a reference.
187198
new_list = list;
@@ -191,6 +202,5 @@ let list_ref;
191202
println!("{list_ref:?}");
192203
```
193204

194-
Alternatively, we could just remove the scope.
195-
There are also nicer ways to do things like this, which we'll look into soon.
196-
Happy coding!
205+
Alternatively, we could just remove the scope. There are also nicer ways to do
206+
things like this, which we'll look into soon. Happy coding!

sections/00_rust_concepts/01_rust_ownership_borrowing/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"defaultFile": "src/main.rs",
33
"source": "https://github.com/Cratecode/rust/tree/master/sections/00_rust_concepts/01_rust_ownership_borrowing",
44
"showShareButton": true
5-
}
5+
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"type": "lesson",
3-
"id": "les_rust_ownership_borrowing",
4-
"extends": "basic",
5-
"name": "Rust Ownership and Borrowing",
6-
"unit" : "rust_intro",
7-
"spec": "An example of borrowing and ownership in Rust.",
8-
"class": "tutorial"
2+
"type": "lesson",
3+
"id": "les_rust_ownership_borrowing",
4+
"extends": "basic",
5+
"name": "Rust Ownership and Borrowing",
6+
"unit": "rust_intro",
7+
"spec": "An example of borrowing and ownership in Rust.",
8+
"class": "tutorial"
99
}

0 commit comments

Comments
 (0)