-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2ea6231
Showing
74 changed files
with
2,484 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Glossary | ||
|
||
| term | explanation | | ||
| ---- | ----------- | | ||
| ahead-of-time compilation | compiling beforehand | | ||
| binary executable | a file that a computer already understands | | ||
| cargo | Rust’s official build system and package manager | | ||
| crate | a bundle of code that you can use in your project | | ||
| crates.io | the official Rust package registry | | ||
| drop | automatically freeing memory for a variable or other resource | | ||
| macro | it’s like a global function | | ||
| rustacean | a Rust community member | | ||
| rustup | the official Rust version manager | | ||
| turbofish | `::<T>` lets you explicitly specify the type | | ||
| wasm | Web Assembly | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Install Rust | ||
|
||
## Installing manually | ||
|
||
* I recommend you to use `rustup` (instructions below). | ||
* If you want to do it the hard way—manually—see [this](https://www.rust-lang.org/tools/install/) | ||
|
||
|
||
## rustup | ||
* Use this one to install any Rust version. | ||
`curl https://sh.rustup.rs -sSf | sh` | ||
|
||
* You can even uninstall Rust: | ||
`rustup self uninstall` | ||
|
||
* Let's see the Rust documentation offline: | ||
`rustup doc` | ||
|
||
* You can update to a newer Rust version (*if you'd like*): | ||
`rustup update` | ||
|
||
|
||
## Solutions to possible problems | ||
|
||
### VSCode integration problems | ||
* **If you see these errors:** | ||
* Couldn't start client Rust Language Server. | ||
* Rustup not available. | ||
* **Add this to your config:** | ||
```json | ||
"rust-client.rustupPath": "~/.cargo/bin/rustup", | ||
``` | ||
|
||
### Toolchain running problems | ||
* **If you see the error when running `cargo` on OS X:** | ||
* `"Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib" cargo` | ||
* **Change your OpenSSL version:** | ||
```bash | ||
brew switch openssl 1.0.2r | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Mechanics | ||
|
||
## Error Handling | ||
|
||
* Rust has a built in generic enum called Result. | ||
* It allows us to return a value that has the possibility of failing. | ||
* It is the idiomatic way in which the language does error handling. | ||
|
||
```rust | ||
enum Result<T,Q>{ | ||
Ok(T), | ||
Err(Q) | ||
} | ||
``` | ||
|
||
### Example: | ||
|
||
```rust | ||
fn might_fail(i:i32) -> Result<f32,String> { | ||
if i == 42 { | ||
Ok(13.0) | ||
} else { | ||
Err(String::from("this is not the right number")) | ||
} | ||
} | ||
``` | ||
|
||
#### Verbose Way: | ||
|
||
```rust | ||
fn main() { | ||
let result = do_something_that_might_fail(12); | ||
match result { | ||
Ok(v) => println!("found {}", v), | ||
Err(e) => println!("Error: {}",e), | ||
} | ||
} | ||
``` | ||
|
||
#### Concise Way: | ||
|
||
```rust | ||
fn main() -> Result<(), String> { | ||
let v = do_something_that_might_fail(42)?; | ||
println!("found {}", v); | ||
Ok(()) | ||
} | ||
``` | ||
|
||
#### Bad Way: | ||
|
||
**Don't use `unwrap()`.** | ||
|
||
```rust | ||
fn main() -> Result<(), String> { | ||
// concise but assumptive and gets ugly fast | ||
let v = do_something_that_might_fail(42)?; | ||
println!("found {}", v); | ||
|
||
// this will panic! | ||
let v = do_something_that_might_fail(1)?; | ||
println!("found {}", v); | ||
|
||
Ok(()) | ||
} | ||
``` | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Resources | ||
|
||
## Getting Started | ||
|
||
* [Intorust Screencasts](http://intorust.com/) | ||
* [Tour of Rust](https://tourofrust.com/) | ||
* [Learning Rust](https://learning-rust.github.io/) | ||
* [A Half-Hour to Learn Rust](https://fasterthanli.me/blog/2020/a-half-hour-to-learn-rust/) | ||
* [Let's build a Guessing Game](https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html) | ||
* [Read Rust: Getting Started](https://readrust.net/getting-started) | ||
* [Rust 101](https://www.ralfj.de/projects/rust-101/main.html) | ||
* [Exercism Rust Track](https://exercism.io/tracks/rust) | ||
* [Web Dev with Rocket](https://christine.website/blog/how-i-start-rust-2020-03-15) | ||
|
||
## Getting Help | ||
|
||
* [The #beginners channel on Discord](https://discord.gg/rust-lang) | ||
* [Users forum](https://users.rust-lang.org/) | ||
* **IRC:** | ||
* [#rust](http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust) | ||
* [#rust-gamedev](http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust-gamedev) | ||
* [#rust-osdev](http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust-osdev) | ||
* [#rust-webdev](http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust-webdev) | ||
|
||
## Resources | ||
|
||
* [Official Learning Resources](https://www.rust-lang.org/learn) | ||
* [Official Rust Book](https://doc.rust-lang.org/book) | ||
* [Read Rust](https://readrust.net/) | ||
* [Learning Resources](https://github.com/ctjhoa/rust-learning) ★ | ||
* [Awesome Rust](https://github.com/rust-unofficial/awesome-rust) | ||
* [Rust Style Guidelines](https://doc.rust-lang.org/1.0.0/style/README.html) | ||
* [Rust by Example](https://doc.rust-lang.org/stable/rust-by-example/) | ||
* [Code Explainer](https://jrvidal.github.io/explaine.rs/) | ||
|
||
## Bloggers | ||
|
||
* [This Week in Rust](https://this-week-in-rust.org/) | ||
* [Niko Matsakis::Baby Steps](http://smallcultfollowing.com/babysteps/) | ||
* [Mara's Blog](https://blog.m-ou.se/) | ||
* [llogiq](https://llogiq.github.io/) | ||
* [Christine Dodrill](https://christine.website/blog) | ||
* [Barely Functional](https://blog.mgattozzi.dev/) | ||
* [In Pursuit of Lazziness](https://manishearth.github.io/) | ||
* [seanmonstar](https://seanmonstar.com/) | ||
* [Stjepan's blog](https://stjepang.github.io/) | ||
|
||
## Articles | ||
|
||
* [References in Rust](https://blog.thoughtram.io/references-in-rust/) | ||
* [Why Rust?](https://christine.website/blog/why-rust-2020-02-15) | ||
* [Rust's runtime](https://blog.mgattozzi.dev/rusts-runtime/) | ||
* [What's a lang item?](https://manishearth.github.io/blog/2017/01/11/rust-tidbits-what-is-a-lang-item/) | ||
* [Writing Python inside your Rust code](https://blog.m-ou.se/writing-python-inside-rust-1/) | ||
* [Rust Disassembly](https://giordi91.github.io/post/disassemlbyrust1/) | ||
* [Porting Godot Games to Rust](https://paytonrules.com/post/games-in-rust-with-godot-part-one/) | ||
* [Why Rust?](http://rust-class.org/0/pages/using-rust-for-an-undergraduate-os-course.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Rust Book | ||
|
||
## What could be better? | ||
|
||
**Tell me the notable features of Rust earlier!** | ||
|
||
* Spends too much time telling about the basics (_chapter 1-6_). However, I believe the real audience of Rust is not novices, but veterens. | ||
|
||
* Also, the book spends a lot of time explaining the module, crate, package structure (_chapter 7_) way before they are really needed. I don't care about the facade (re-exporting). | ||
|
||
* I remember wondering about what are traits from the beginning. | ||
|
||
## The most useful chapter was these ones: | ||
* Programming a Guessing Game | ||
* Understanding Ownership | ||
|
||
## What should have included from the start? | ||
* A tour of stdlib: io, env, net, idk?! | ||
* Rust editions could be clarified. IDK what are they. | ||
|
||
--- | ||
|
||
## Chapter 1: Getting Started | ||
|
||
* `rustup` installation OK. | ||
* Don't talk about `rustc`, just tell me `cargo [new|run]`. | ||
* Unnecessarily talking about `Cargo.toml`. Tell me you're going to explain me later. And you do in the Guessing game chapter. | ||
|
||
## Chapter 2: Programming a Guessing Game | ||
|
||
* Awesome chapter, nothing to say. | ||
|
||
## Chapter 3: Common Programming Concepts | ||
|
||
* The worst chapter ever. | ||
* Explains the basic concepts very DRY. As if it's a spec of the language... | ||
|
||
## Chapter 4: Understanding Ownership | ||
|
||
* Awesome chapter. | ||
* Talks about stack/heap difference way earlier. Not needed at this stage. Talk about it when talking about copying integers (_where there is no need clone them_) etc. | ||
* Why explain slices here? Explain where it is needed, later on. | ||
|
||
## Chapter 5: Structs | ||
|
||
* Another boring chapter. Dry like the 2nd one. | ||
* Methods and the Rectangle example at the end were enough. | ||
|
||
## Chapter 6: Enums and Pattern Matching | ||
|
||
* Dry... Boring... Although there are interesting features like `match`, data binding to `enum`s, etc. These aspects should have been highlighted more. | ||
|
||
## Chapter 7: Managing Growing Projects with Packages, ... | ||
|
||
* WTF?! Why is this here? I didn't create a big project yet. Why should I want to manage my "growing" project? Not yet. Explain me later on. | ||
|
||
* Why do you talk about the glob operator if it's often used when testing! | ||
|
||
## Chapter 8: Common Collections | ||
|
||
* "deref coercion": Too much unnecessary complexity at this point (which is a topic of Chapter 15!). So, just show me what can be done and not without explaining the inner details yet. I can look them up if I need to. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Shocks | ||
|
||
Weird Rust stuff documented. | ||
|
||
1. **There is no null in Rust!** | ||
|
||
Instead, there is `None`, and there is: | ||
|
||
```rust | ||
enum Option<T>{ | ||
Some(T), | ||
None | ||
}``` | ||
|
||
2. **String `AsRef<Path>`** | ||
|
||
Why the heck `String` has a responsibility for `Path`... | ||
|
||
3. **Why Rust doesn't allow marking only some struct fields mutable?** | ||
|
||
Each field is in a different memory location. However, the struct | ||
is a type. So, when using it, we can't know which part of the | ||
struct's instance that the other code is going to use. | ||
|
||
4. **I couldn't grasp how to run multiple files together from get-go** | ||
|
||
src/main.rs: | ||
|
||
mod vectors; | ||
fn main() { | ||
vectors::run_vectors(); | ||
} | ||
|
||
src/vectors.rs: | ||
|
||
pub fn run_vectors() { | ||
println!("vectors!"); | ||
} | ||
|
||
Best explanation: | ||
https://learning-rust.github.io/docs/d3.modules.html | ||
|
||
5. **String concatenation uses the buffer of the origin string** | ||
|
||
This is a good thing as in the op below, Rust will copy str2 to | ||
str1's buffer. | ||
|
||
str1 + &str2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Tips | ||
|
||
|
||
## General | ||
|
||
1. Create SMALL rust binaries, see: [min-sized-rust](https://github.com/johnthagen/min-sized-rust). | ||
2. Compile simple programs w/`rustc`, but larger programs w/`cargo`. | ||
3. Format your code automatically: `rustfmt`. | ||
|
||
|
||
## Cargo | ||
|
||
1. Creates a new crate (it's like `go mod init`): | ||
`cargo new <name>` | ||
|
||
Without a git repository: | ||
`cargo new <name> --vcs none` | ||
|
||
2. Build the crate (it's like `go create`): | ||
`cargo build` | ||
`cargo b` | ||
|
||
3. Run the crate (it's like `go run`): | ||
`cargo run` | ||
`cargo r` | ||
|
||
1. Silently run: | ||
`cargo r -q` | ||
|
||
4. Check whether your program can compile: | ||
`cargo check` | ||
`cargo c` | ||
|
||
Rustaceans use this command to periodically check their programs. After working on their program, and when they're sure, they build your program using `cargo build --release`. | ||
|
||
This command produces a faster program, but it'll take a longer time to compile. | ||
|
||
This is also the command you want to use when you benchmark your code. | ||
|
||
5. Cargo caches. If you want to start on a clean slate, run: | ||
`cargo clean` | ||
|
||
Why? Sometimes, `cargo check` returns a warning. However, when you run it the second time, it doesn't display the warning. In that case, you can run `cargo clean`, and then run `cargo check` again. This way, you'll be able to see the error message again. | ||
|
||
6. You can see the documentation of every crate that your program/package depends on. | ||
`cargo doc --open` | ||
|
||
7. Create a library package. | ||
`cargo new --lib <name>` | ||
_This command will create a package with a test._ | ||
|
||
8. Work on someone else's project: | ||
```bash | ||
git clone url/project | ||
cd project | ||
cargo build | ||
``` | ||
|
||
## Memory Leaks | ||
|
||
1. Use [Valgrind](https://www.valgrind.org/info/) to check for memory errors. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Useless Knowledge | ||
|
||
## Rust's name? | ||
|
||
Graydon is Rust's creator. | ||
|
||
<graydon> I think I named it after fungi. rusts are amazing creatures. | ||
<graydon> Five-lifecycle-phase heteroecious parasites. I mean, that’s just _crazy_. | ||
<graydon> talk about over-engineered for survival |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Rust Programming Language Book | ||
|
||
I'm learning Rust, and here are my notes. In each program, you'll find extensive explanations about everything. | ||
|
||
## Index | ||
|
||
1. [Install Rust](../notes/install.md) | ||
2. [First Program](rpl/hello) | ||
3. [First Cargo Program](rpl/hello_cargo) | ||
4. [Guessing Game](rpl/guessing_game) | ||
|
||
## Other Stuff | ||
|
||
* [Weird Things about Rust](../notes/shocks.md) | ||
* [Useless Knowledge](../notes/useless.md) | ||
* [Some Rust Tips](../notes/tips.md) | ||
* [Some Rust Mechanics Explained](../notes/mechanics.md) | ||
* [Rust Glossary](../notes/glossary.md) | ||
* [Rust Comparison to Some Other Langs](../notes/langs.png) | ||
* [Awesome Rust Resources](../notes/resources.md) |
Oops, something went wrong.