Just me learning Rust by doing the tutorial exercises.
brew install rustup
rustup-init
Use the defaults then, once that's done also do
rustup completions bash > $(brew --prefix)/etc/bash_completion.d/rustup.bash-completion
source $HOME/.cargo/env
check it all worked with
rustc --version
You should get something like
~$ rustc 1.25.0 (84203cac6 2018-03-25)
Now install cargo-edit
cargo install cargo-edit
Let that complete.
Now you can add dependencies to your project using
cargo add <some-library>
instead of having to look up the speccific library version and manually edit the Cargo.toml
file.
Then add rustfmt-preview
to your toolchain.
rustup component add rustfmt-preview
Now you can run the rust code formatter (aka linting)
rustup update
Create a new application project
cargo new <project_name>
Create a new library project
cargo new <project_name> --lib
Generate project documentation (including for dependencies) and open the docs in your browser
cargo doc --open
Build a project
cargo build
Run the debug version of the built project
cargo run
Build and run the release version of the project
cargo build --release
cargo run --release
Run tests (when there are tests to run)
cargo test
Linting
cargo fmt -- --write-mode=diff
-
A
String
is just a vector of 8-byte chars -
String constants can't be passed in to macros.
so you can't do
println!(SOME_STRING_CONSTANT)
but you can do
fn printer(msg: &str) { println!("{}", msg); } printer(&SOME_STRING_CONSTANT);
but functions can't have default or optional params (that I can work out anyway)
You can't do something like
fn printer(msgFmt: &str, val: u32) { println!(&msgFmt, val); }
Length | Signed | Unsigned |
---|---|---|
8-bit | i8 | u8 |
16-bit | i16 | u16 |
32-bit | i32 | u32 |
64-bit | i64 | u64 |
arch | isize | usize |
f32
and f64
. f64
is the default.
let tup: (i32, f64, u8) = (500, 6.4, 1);
// with destructuring
let (x, y, z) = tup;
// or via dot notation
let n = tup.0; // gives 500
Arrays are fixed. If you need to add or remove items from a list use a vector
.
let a = [1, 2, 3, 4, 5];
let one = a[0];
Constants must have a type.
const A_NUMBER: u32 = 153;
const A_CONSTANT: &str = "This can't be changed or shadowed.";
Variables are immutable by default
let my_string = "this can't be changed directly";
but can be shadowed
let my_string = "but it can be shadowed via use of another let"
let my_string = my_string.len() // including being shadowed by a new type.
Variables can use mut
to declare them as mutable.
let mut my change_me = "this can be changed"
change_me = "but not to a new type"
If you add a semicolon to the end of an expression, you turn it into a statement, which will then not return a value.
So
{
let x = 3;
x + 1
}
is an expression that evaluates to 4
fn five() -> i32 {
5
}
let x = five();
fn add_one(n: i32) -> i32 {
n + 1
}
let y = add_one(x);
fn some_test(is_it: bool) -> i32 {
if is_it {
1
} else }
0
}
}
let x = is_it(true) // 1
let y = is_it(false) // 0
- Rust was originally developed by Graydon Hoare who now wroks at Apple on the Swift language.
- Graydon is a biology nerd.
- Rust is named after a fungus that is robust, distributed, and parallel.
- A Rust developer is known as a 'Rustacean' (rhymes with crustacean)