Collection of Rust code examples for my blog F7L8C9.
Licensed under the MIT License. See file LICENSE.
Rust build tool and package manager. Select a Rust edition in the Cargo.toml
file of your package. An edition is a curated set of dependencies that work well
together. Somewhat like a Linux distribution.
Building with Cargo assumes that a linker has been installed already. If not, it
can be installed on Linux with sudo apt install build-essential
.
cargo new <package_name>
--bin
to create a binary program--lib
to create a library--vcs none
to skip Git repository initializationcargo build
cargo run
cargo test
rustfmt
against package with cargo fmt
cargo doc
cargo publish
cargo update
(updates Cargo.lock
file)Crate registry is at crates.io. Documentation for crates
is at Docs.rs.
If you’re building a non-end product, such as a rust library that other rust
packages will depend on, put Cargo.lock
in your .gitignore
. If you’re
building an end product, which are executable like command-line tool or an
application, or a system library with crate-type of staticlib or cdylib, check
Cargo.lock
into git. See Cargo FAQ.
Show the active and installed toolchains or profiles with rustup show
.
Check for updates to toolchains and rustup with rustup check
.
Update environment to latest stable version with rustup update stable
.
Use wasm-pack build
to create a package.
Rust allows trailing commas.
struct User {
name: String,
age: u8,
}