These are notes for a quick introduction to Rust.
Overview Link to heading
- Statically typed & Compiled language.
- Great developer experience:
cargobuild systemrust-analyzerLSP
Rust features Link to heading
Basics Link to heading
| C++ | Rust |
|---|---|
| std::size_t | usize |
| std::pointerdiff_t | isize |
| int | i32 |
| unsigned int | u32 |
| long long | i64 |
| unsigned long long | u64 |
| string | String |
| string_view | &str |
| byte | u8 |
| char | char |
| vector<T> | Vec<T> |
| array<int, 4> | [u32; 4] |
| int[] | &[u32] |
| T | T |
| const T& | &T |
| T& | &mut T |
| T* | unsafe { T* } |
| unique_ptr<T> | Box<T> |
| optional<T> | Option<T> |
| variant<T, E> | Result<T, E> |
| C++ | Rust |
|---|---|
| for(int i = 0; i < n; ++i) {} | for i in 0..n {} |
| while(true) {} | loop {} |
| while(f()) {} | while f() {} |
| do { } while (f()); | loop { if !f() { break; } } |
| switch x { case 1:; } | match x { 1 => {} } |
| C++ | Rust |
|---|---|
| cout << “text” << endl; | println!(“text”); |
| cout << 1+1 << endl; | println!("{}", 1+1); |
| cout << n << endl; | println!("{n}"); |
Basic syntax Link to heading
| |
Expressions everywhere! Link to heading
| |
Closures Link to heading
| |
Pattern matching Link to heading
| |
References Link to heading
Ownership Link to heading
Containers Link to heading
| |
Traits Link to heading
| |
Iterators Link to heading
| |
Common libraries Link to heading
See blessed.rs for a list of commonly used and recommended libraries.
- rand: random number generation.
- clap: Command Line Argument Parsing.
- serde: SERialization and Deserialization to json, yaml, and many other formats.
- itertools: Extra utilities for iterating over stuff.
- coloured: coloured terminal output.
Ecosystem Link to heading
- Release cycle
- Unstable rust
cargo {build,run} -rfor release mode is much faster.cargo add <crate>to add a dependency from CLI.- Many tools, like
cargo flamegraphfor profiling
Useful links Link to heading
There is a lot of high quality documentation:
First, rust-lang.org/learn contains a lot of useful links, some of which I replicate here:
- The Rust book, doc.rust-lang.org/book
- A gentle step by step introduction to the
Rust language and ecosystem.
Check out the page on Control Flow and find something that you’ve not seen in other languages.
- The reference, doc.rust-lang.org/reference
- A more formal documentation of
language features. Probably not so readable for beginners.
Find the page on Traits.
- Documentation, doc.rust-lang.org/std
- The standard library docs. Always keep
this close by, and consider making a hotkey for searching it ;)
Read some of the docs for
fnandprintln!. - Crate registry, crates.io
- Where all public crates (packages) are. Useful
for searching dependencies.
Try searching
cli, and make sure to sort by All-Time Downloads. Find the github page and documentation of the first result. - Crate documentation, docs.rs
- Documentation for all crates!
Search for
serdeand go to its docs. Find documentation for theSerializetrait. Is an array of length 64 serializable? Also you can find the corresponding crates.io page. - Comprehensive Rust, https://google.github.io/comprehensive-rust/
- Introduction by Google
Hands-on Link to heading
Installation Link to heading
Go to https://rustup.rs and follow instructions.
- Arch Linux alternatively has the
rustuppackage.
Also install Rust analyzer binary, the LSP.
rust-analyzervscode extensionrust-analyzerpackage in your package manager.- via rustup:
rustup component add rust-analyzer
Make sure to enable the LSP in your IDE.
Recommended: install GitHub copilot as well.
Create a project Link to heading
Go to your projects folder, and run cargo new hello_world. This creates a new
project:
| |
Hello, world! Link to heading
Currently, main.rs looks like this:
| |
fnis the syntax to introduce a new function.fn maincreates themainfunction, the entry point of a binary.main()takes no arguments.println!is a macro (i.e. not a regular function call) that prints its argument to standard output.
To run the program, simply do cargo run from anywhere in the projects directory.
- This will first build the code (see
/target/), if not already done. - It will then run the binary.
Note that cargo is the package manager and build system (and more).
cargo invokes rustc, the underlying compiler.
Small project ideas Link to heading
- Compute all minimizers of a string.
- Solve some Project Euler problems
- Write a guessing game: the program chooses a random number and the user has to guess it with lower/correct/higher answers.