These are notes for a quick introduction to Rust.
Overview
- Statically typed & Compiled language.
- Great developer experience:
cargo
build systemrust-analyzer
LSP
Rust features
Basics
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
|
|
Expressions everywhere!
|
|
Closures
|
|
Pattern matching
|
|
References
Ownership
Containers
|
|
Traits
|
|
Iterators
|
|
Common libraries
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
- Release cycle
- Unstable rust
cargo {build,run} -r
for release mode is much faster.cargo add <crate>
to add a dependency from CLI.- Many tools, like
cargo flamegraph
for profiling
Useful links
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
fn
andprintln!
. - 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
serde
and go to its docs. Find documentation for theSerialize
trait. 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
Installation
Go to https://rustup.rs and follow instructions.
- Arch Linux alternatively has the
rustup
package.
Also install Rust analyzer binary, the LSP.
rust-analyzer
vscode extensionrust-analyzer
package 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
Go to your projects folder, and run cargo new hello_world
. This creates a new
project:
|
|
Hello, world!
Currently, main.rs
looks like this:
|
|
fn
is the syntax to introduce a new function.fn main
creates themain
function, 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
- 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.