Rust - notes
- Installation
- IDEs for Rust
- Dead code
- Questions
- Documentation
- The main function
- let statement and variables in Rust
- constants
- shadowing
- String type
- Associated function
- Variables and Mutability - immutable by default
- Variable references
- Core principles of Rust language
- References
- Tags
- TODO
- Resources to learn Rust
- Interesting projects
Installation
https://doc.rust-lang.org/cargo/getting-started/installation.html
curl https://sh.rustup.rs -sSf | sh
IDEs for Rust
- Install rust-rover IDE.
- Use Jetbrains toolbox to install it.
- Install the
rust-analyzer
plugin for VSCode to be able to run programs quickly.
Tools that I tried with no luck
- Install the rust plugin for IntelliJ idea to be able to run programs quickly.
- The plugin doesn’t seem to be available anymore.
- Eclipse Corrosion: https://projects.eclipse.org/projects/tools.corrosion
- Stay away from this. This is crashing the entire system.
- RustDT Eclipse plugin: https://github.com/RustDT/RustDT
- Did not work for me. It complained that something is missing.
Dead code
https://doc.rust-lang.org/rust-by-example/attribute/unused.html
Questions
Are modules analogous to Java packages?
Documentation
The installation of Rust also includes a copy of the documentation locally, so you can read it offline. Run rustup doc
to open the local documentation in your browser.
The main function
The main function is special. It is always the first code that runs in every executable Rust program.
let statement and variables in Rust
https://doc.rust-lang.org/stable/book/ch03-01-variables-and-mutability.html
We use the let statement to create variables.
e.g.
let apples = 5;
let mut guess = String::new();
constants
https://doc.rust-lang.org/stable/book/ch03-01-variables-and-mutability.html
shadowing
https://doc.rust-lang.org/stable/book/ch03-01-variables-and-mutability.html
String type
https://doc.rust-lang.org/stable/std/string/struct.String.html String is a string type provided by the standard library that is a growable, UTF-8 encoded bit of text.
Associated function
The ::
syntax in the ::new
line indicates that new is an associated function of the String
type. An associated function is a function that’s implemented on a type, in this case String
. This new
function creates a new, empty string. You’ll find a new
function on many types because it’s a common name for a function that makes a new value of some kind.
Variables and Mutability - immutable by default
In Rust, variables are immutable by default. To make a variable mutable, we add mut
before the variable name:
let apples = 5; // immutable
let mut bananas = 5; // mutable
Variable references
The &
indicates that the argument is a reference, which gives you a way to let multiple parts of your code access one piece of data without needing to copy that data into memory multiple times. References are a complex feature, and one of Rust’s major advantages is how safe and easy it is to use references. Like variables, references are immutable by default.
Rust has a number of types named Result in its standard library: a generic Result as well as specific versions for submodules, such as io::Result. The Result types are enumerations, often referred to as enums, which can have a fixed set of possibilities known as variants. Enums are often used with match, a conditional that makes it convenient to execute different code based on which variant an enum value is when the conditional is evaluated.
The purpose of these Result types is to encode error-handling information.
Result’s variants are Ok and Err. The Ok variant indicates the operation was successful, and inside Ok is the successfully generated value. The Err variant means the operation failed, and Err contains information about how or why the operation failed.
Values of the Result type, like values of any type, have methods defined on them. An instance of io::Result has an expect method that you can call. If this instance of io::Result is an Err value, expect will cause the program to crash and display the message that you passed as an argument to expect. If the read_line method returns an Err, it would likely be the result of an error coming from the underlying operating system. If this instance of io::Result is an Ok value, expect will take the return value that Ok is holding and return just that value to you so you can use it. In this case, that value is the number of bytes in the user’s input.
If you don’t call expect, the program will compile, but you’ll get a warning.
Core principles of Rust language
- Memory safety without garbage collection - https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html
- Concurrency without data races - https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html
- Abstraction without overhead - https://blog.rust-lang.org/2015/05/11/traits.html
References
- https://docs.rs/sum_type/latest/sum_type/
- https://doc.rust-lang.org/std/boxed/struct.Box.html
- https://doc.rust-lang.org/std/boxed/
- https://tonyarcieri.com/a-quick-tour-of-rusts-type-system-part-1-sum-types-a-k-a-tagged-unions
Tags
- Rust - why learn the language
- Rust - Boxing and Unboxing
- Rust - cargo commands
- Rust - Standard library and crates
- Rust - Standard IO
- Rust - Traits
- Rust - Embracing Functional Programming Patterns
- Zero Cost Abstractions
- Rust - creating a project
TODO
- Ownership node: https://blog.skylight.io/rust-means-never-having-to-close-a-socket/
- benefits of the language
- current state of frameworks
- use cases
- rewriting enterprise applications using Rust
Resources to learn Rust
- The Rust Programming Language
- Jeremy Chone on YouTube: https://www.youtube.com/c/JeremyChone
- Jeremy has some great free content and is easy to get in touch with on his Discord if you have questions.
- His content is dense.
- Jon Gjengset on YouTube: https://www.youtube.com/c/JonGjengset
- Insanely good
- Crust of Rust playlist: https://www.youtube.com/playlist?list=PLqbS7AVVErFiWDOAVrPt7aYmnuuOLYvOa
- A very good Rust YouTube and mentor.
- One reason why Gjengset is such a hero. He obviously enjoys the language, and prefers it, but he also has things he doesn’t like and critisizes.
- fasterthanlime on YouTube
- Comprehensive Rust
- I found this “Comprehensive Rust” course made by the Android team at Google that looks really good.
- It seems to be good and it has exercises along the way
- https://google.github.io/comprehensive-rust/index.html
- Learn Rust with Rustlings
- Once you get to intermediate level, check out Rust Atomics and Locks. There is a print version that you can buy but there is also a free version on her website.
- https://medium.com/@luishrsoares/list/mastering-rust-programming-06763d788b6e
- https://www.linkedin.com/in/luishsoares/recent-activity/articles/
- https://docs.rs/sum_type/latest/sum_type/
- https://doc.rust-lang.org/std/boxed/struct.Box.html
- https://doc.rust-lang.org/std/boxed/
- https://tonyarcieri.com/a-quick-tour-of-rusts-type-system-part-1-sum-types-a-k-a-tagged-unions