Skip to content

Commit

Permalink
Add Rust enums lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
uellenberg committed May 23, 2024
1 parent 29056f2 commit ad7cc5e
Show file tree
Hide file tree
Showing 17 changed files with 482 additions and 4 deletions.
6 changes: 5 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"name": "Introduction to Rust",
"upload": [
"sections/00_rust_concepts",
"sections/01_rust_projects"
"sections/01_rust_projects",
"sections/02_rust_project_concepts"
],
"templates": "templates",
"images": "images",
Expand All @@ -14,6 +15,9 @@
},
"rust_projects": {
"next": []
},
"rust_project_concepts": {
"next": []
}
},
"configTemplate": {
Expand Down
20 changes: 20 additions & 0 deletions sections/00_rust_concepts/03_rust_syntax/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ let mut my_mutable_variable = 10;
my_mutable_variable = 100;
```

Rust has support for something called variable shadowing. That means that we can re-declare variables, even if one with the same name already exists, and the new variable will hide (*case a shadow over*) the variable that already exists. Keep in mind that both variables still exist, and any references to them are still valid; we've just hidden the first one.

```rust
let my_variable = 2;
let my_variable_ref = &my_variable;

// This shadows my_variable by creating a **new**
// variable with the same name.
let my_variable = 5;

// However, both variables still exist,
// and have separate data.
// We just can't access the original under its
// name anymore.
println!("{my_variable_ref}"); // 2
println!("{my_variable}"); // 5
```

## Functions

We already took a look at the main function above, so you should have an idea of how they're defined.
Expand All @@ -78,6 +96,8 @@ Then, if we want to specify what the function returns (if it returns anything, w
the `->` is used.
In this case, `-> bool` means it will return a boolean.

Keep in mind that, if you don't specify a return type, the function will still have one - it will just default to the type `()`. This is called the **unit type**, and is essentially a type that doesn't have any data in it. Because it has no data, you don't have to return `()` at the end of your function, but you can if you want to. There are a few places that this becomes useful, but the biggest part that you should worry about right now is in error messages. Sometimes, Rust will say that it expected `()`, and all that means is "no data". Similarly, you might see `Ok(())` or `Result<(), Error>`, and it means exactly the same thing - no data (for this example, it means the code succeeded, but isn't meant to return any data).

## Control Flow

### If Statements
Expand Down
3 changes: 3 additions & 0 deletions sections/01_rust_projects/010_more_rust_concepts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# More Rust Concepts

There are a few more lessons on Rust concepts that weren't covered by the ones that you just went through. I'd highly encourage you to go through a project, since concepts are introduced to you as you use them, but if you just want to go through the rest of the articles, this is the place to be! Click the next button to head over to the next article.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"defaultFile": "src/main.rs",
"source": "https://github.com/Cratecode/rust/tree/master/sections/01_rust_projects/010_mandelbrot_set_renderer"
"source": "https://github.com/Cratecode/rust/tree/master/sections/01_rust_projects/020_more_rust_concepts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "lesson",
"id": "les_more_rust_concepts",
"extends": "basic",
"name": "More Rust Concepts",
"unit" : "rust_intro"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"defaultFile": "src/main.rs",
"source": "https://github.com/Cratecode/rust/tree/master/sections/01_rust_projects/020_mandelbrot_set_renderer"
}
12 changes: 10 additions & 2 deletions sections/01_rust_projects/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
"id": "rust_projects",
"name": "Rust Projects",
"upload": [
"010_mandelbrot_set_renderer"
"010_more_rust_concepts",
"020_mandelbrot_set_renderer"
],
"lessons": {
"les_more_rust_concepts": {
"next": [
"rust_project_concepts"
]
},
"les_rust_mandelbrot_set_renderer": {
"next": []
"next": [
"les_rust_enums"
]
}
}
}
75 changes: 75 additions & 0 deletions sections/02_rust_project_concepts/01_rust_enums/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions sections/02_rust_project_concepts/01_rust_enums/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "project"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
Loading

0 comments on commit ad7cc5e

Please sign in to comment.