This presentation roughly follows The Rust Programming Language also called The Book by Steve Klabnik and Carol Nichols with contributions from the Rust Community
You can generate an offline version of The Book with
$ rustup docs --book
Linux
and macOS
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
You’ll also need a linker that usually comes with a
C
compiler. So, yeah, get your distro’s build tools.
Windows
Visit the installation page and follow along.
Just like with
*nix
, you’ll need some build tools. They usually come withVisual Studio
.
Compiler
$ rustc --version
rustc x.y.z (acabacab yyyy-mm-dd)
Tooling
$ cargo --version
cargo x.y.z (acabacab yyyy-mm-dd)
Updating
$ rustup update
Uninstalling
$ rustup self uninstall
$ cargo new foo && cd foo && tree
.
├── Cargo.toml
└── src
└── main.rs
2 directories, 2 files
Cargo.toml
src/main.rs
$ cargo run
Compiling foo v0.1.0 (/path/to/the/project/foo)
Finished dev [unoptimized + debuginfo] target(s) in 6.11s
Running `target/debug/foo`
Hello, world!
{...}
in format string{:?}
(Debug formatting)$ cargo run
Here is some Rust wisdom: 1312
fn main() {
let x = 1312;
println!("Here is some Rust wisdom: {x}");
x = 1337;
println!("Here is more: {x}");
}
$ cargo run
error[E0384]: cannot assign twice to immutable variable `x`
--> src/main.rs:5:5
|
2 | let x = 1312;
| -
| |
| first assignment to `x`
| help: consider making this binding mutable: `mut x`
...
5 | x = 1337;
| ^^^^^^^^ cannot assign twice to immutable variable
fn main() {
let mut x = 1312;
println!("Here is some Rust wisdom: {x}");
x = 1337;
println!("Here is more: {x}");
}
$ cargo run
Here is some Rust wisdom: 1312
Here is more: 1337
fn main() {
let x = 5;
let x = x + 1;
{
let x = x * 2;
println!("The value of x in the inner scope is: {x}");
}
println!("The value of x is: {x}");
}
$ cargo run
The value of x in the inner scope is: 12
The value of x is: 6
$ cargo run
✦ ❯ cargo run
--> src/main.rs:2:9
|
2 | let guess = "42".parse().expect("Not a number!");
| ^^^^^ ----- type must be known at this point
|
= note: cannot satisfy `<_ as FromStr>::Err == _`
help: consider giving `guess` an explicit type
|
2 | let guess: /* Type */ = "42".parse().expect("Not a number!");
| ++++++++++++
Length | Signed | Unsigned |
---|---|---|
8-bit | i8 | u8 |
16-bit | i16 | u16 |
32-bit | i32 | u32 |
64-bit | i64 | u64 |
128-bit | i128 | u128 |
arch | isize | usize |
Number literals | Example |
---|---|
Decimal | 98_222 |
Hex | 0xff |
Octal | 0o77 |
Binary | 0b1111_0000 |
Byte (u8 only) | b’A’ |
Floating points
f32
and f64
Always signed!
Booleans
true
and false
and maybe
Characters
'a'
, 'Z'
, '🦀'
UTF-8 encoded
#[derive(Debug)]
struct Player {
position: (i32, u32),
name: String,
}
fn main() {
let player = Player {
position: (0, 0),
name: String::from("Jhon"),
};
println!("{player:?}")
}
$ cargo run
Player { position: (0, 0), name: "Jhon" }
#[derive(Debug)]
enum Entity {
Player,
Slime,
}
fn main() {
let player1 = Entity::Player;
let enemy = Entity::Slime;
println!("Player1 entity: {:?}", player1);
}
$ cargo run
Player1 entity: Player
#[derive(Debug)]
enum Entity {
Player(i32, i32),
Slime(i32, i32),
}
fn main() {
let player1 = Entity::Player(3, 10);
let enemy = Entity::Slime(2, 4);
println!("Player1 at (x:3; y:10): {:?}", player1);
}
$ cargo run
Player1 at (x:3; y:10): Player(3, 10)
struct Player {
position: (i32, u32),
name: String,
}
enum Entity {
Player(Player),
Slime(i32, i32),
}
fn main() {
let player1 = Entity::Player(Player {
position: (3, 10),
name: String::from("Steve"),
});
let enemy = Entity::Slime(2, 4);
println!("Player1: {:?}", player1);
}
$ cargo run
Player1: Player(Player { position: (3, 10), name: "Steve" })
Option
and Result
We won’t talk about the borrowing rules 🙂
fn battle(player: Player, mob: Slime) -> Entity {
let winner = player;
return Entity::Player(player);
}
trait Body {
fn position(&self) -> (i32, i32);
}
fn higher<B0: Body, B1: Body>(body1: B0, body2: B1) -> Ordering {
let (_, b0_y) = body0.position();
let (_, b1_y) = body1.position();
if b0_y < b1_y {
Ordering::Less
} else if b0_y == b1_y {
Ordering::Equal
} else {
Ordering::Greater
}
}
let is_player_winning = higher(player, slime) == Ordering::Greater;