A Quick Start to Rust
This is a quick start to Rust. Its meant to give you an idea of what Rust is like, as well as communicate some high-level ideas behind what makes Rust interesting. Each section will not be comprehensive, that’s for later, but you should get a feel for what its like to write and maintain Rust.
If you’re interested in using Rust after this, there’s more in-depth content on the way over on Rust Adventure.
We’ll start off by having a brief discussion about installing Rust and what tools come with that.
The Rust Playground
But first, if you don’t want to install Rust just yet, you can head over to the Rust Playground. This will let you write and run Rust in the browser with a few restrictions, such as a limited selection of 3rd party crates and limited resources.
Installing Rust
To install Rust on your local system, head to rustup.rs and follow the instructions.
Rustup is how you manage installed Rust versions, and their associated toolchain. This includes tools like Cargo, Rust’s package manager, as well as the tools needed for compiling for different targets, like wasm, and formatting, linting, and documenting Rust code.
Running rustup will show a list of subcommands if you’ve installed it correctly, and it is on your PATH.
The Rust toolchain installer
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
Commands:
toolchain Install, uninstall, or list toolchains
default Set the default toolchain
show Show the active and installed toolchains or profiles
update Update Rust toolchains and rustup
check Check for updates to Rust toolchains and rustup
target Modify a toolchain's supported targets
component Modify a toolchain's installed components
override Modify toolchain overrides for directories
run Run a command with an environment configured for a given toolchain
which Display which binary will be run for a given command
doc Open the documentation for the current toolchain
man View the man page for a given command
self Modify the rustup installation
set Alter rustup settings
completions Generate tab-completion scripts for your shell
help Print this message or the help of the given subcommand(s)
With a version of Rust (and its toolchain) installed, building a Rust program is a matter of running cargo build. This will by default build for your local system, so if you’re on macos, it will build for the macos target.
If we wanted to target another platform, like wasm in the browser instead of our native system, we can add the desired target using Rustup. Here we’ll use the wasm32-unknown-unknown target, since that is a common target to run in the browser and will produce a .wasm file.
rustup target install wasm32-unknown-unknown
And we could use Cargo to build a Rust library into a .wasm file that runs in the browser.
cargo build --target wasm32-unknown-unknown
Cargo
Cargo is Rust’s package manager, but that’s underselling it a bit. Cargo is capable of calling out to a suite of the Rust toolchain tools that we installed with Rustup, as well as community maintained subcommands.
Commands:
build, b Compile the current package
check, c Analyze the current package and report errors, but don't build object files
clean Remove the target directory
doc, d Build this package's and its dependencies' documentation
new Create a new cargo package
init Create a new cargo package in an existing directory
add Add dependencies to a manifest file
remove Remove dependencies from a manifest file
run, r Run a binary or example of the local package
test, t Run the tests
bench Run the benchmarks
update Update dependencies listed in Cargo.lock
search Search registry for crates
publish Package and upload this package to the registry
install Install a Rust binary
uninstall Uninstall a Rust binary
... See all commands with --list
Some of the useful subcommands that call out to components that were installed with Rustup include:
| cargo subcommand | component | purpose |
|---|---|---|
| cargo build | rustc | build |
| cargo clippy | clippy | linter |
| cargo fmt | rustfmt | auto-format |
| cargo doc | rustdoc | build documentation |
Community Subcommands
There are a wide array of community Cargo subcommands as well. These can be installed with cargo install directly from the package registry crates.io.
One example is cargo-generate, which is a more sophisticated package scaffolding tool than the built-in cargo new. The command can be installed with cargo install:
cargo install cargo-generate
and then we can use it to grab a template from the Rust Adventure GitHub Organization, and scaffold the new project locally:
cargo generate rust-adventure/quickstart
Rust Analyzer
Rustup also installs Rust Analyzer.
Rust Analyzer is the Language Server Protocol implementation for Rust. That is to say Rust Analyzer is what provides your IDE or editor of choice with typechecking, diagnostics, code assists, go-to definition, refactoring, code completions, and more.
Rust Analyzer can also power “inlay hints”, which expose the types the data in variables across your program, as well as triggering auto-formatting on save.
One neat trick Rust Analyzer has is converting a string of json into a reasonable Rust data type representation. The following string can be pasted into an editor powered by Rust Analyzer.
{"name":"chris","is_author":true,"pets":["dog"]}
and turned into the following struct definition, which isn’t perfect, but can be a great starting place.
struct Root1 {
is_author: bool,
name: String,
pets: Vec<String>,
}