Rust Basics 🦀
Motivation
Discord's blog on their reasons for switching from Go to Rust piqued my interest. Although Rust has a reputation for being challenging to learn, this only fuels my desire to master it. Moreover, Rust's compatibility with JavaScript through WebAssembly makes it an invaluable tool for offloading resource-intensive operations
Let's Install Rust!
rustup is the installer for Rust. This can be paralleled to nvm in the node world as it controls the installation of Rust on your machine.
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer
Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
/Users/kc/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/Users/kc/.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:
/Users/kc/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/Users/kc/.profile
/Users/kc/.zshenv
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: aarch64-apple-darwin
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1
info: profile set to 'default'
info: default host triple is aarch64-apple-darwin
warning: Updating existing toolchain, profile choice will be ignored
info: syncing channel updates for 'stable-aarch64-apple-darwin'
info: latest update on 2023-02-09, rust version 1.67.1 (d5a82bbd2 2023-02-07)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
55.1 MiB / 55.1 MiB (100 %) 47.7 MiB/s in 1s ETA: 0s
info: downloading component 'rustfmt'
info: removing previous version of component 'cargo'
info: removing previous version of component 'clippy'
info: removing previous version of component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: removing previous version of component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
19.4 MiB / 19.4 MiB (100 %) 7.7 MiB/s in 2s ETA: 0s
info: installing component 'rust-std'
27.6 MiB / 27.6 MiB (100 %) 18.6 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
55.1 MiB / 55.1 MiB (100 %) 20.4 MiB/s in 2s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-aarch64-apple-darwin'
stable-aarch64-apple-darwin updated - rustc 1.67.1 (d5a82bbd2 2023-02-07) (from rustc 1.65.0 (897e37553 2022-11-02))
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, run:
source "$HOME/.cargo/env"
Rust is now installed! Woo!
rustup
Now let's see what rustup has.
$ rustup
SUBCOMMANDS:
show Show the active and installed toolchains or profiles
update Update Rust toolchains and rustup
check Check for updates to Rust toolchains and rustup
default Set the default toolchain
toolchain Modify or query the installed toolchains
target Modify a toolchain's supported targets
component Modify a toolchain's installed components
override Modify directory toolchain overrides
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 Prints this message or the help of the given subcommand(s)
cargo
When you install Rust, cargo gets installed as well. Think of this as the Rust version of npm. You can check your version of cargo using.
$ cargo --version
Go ahead and make an account on https://crates.io/! This is the equivalent to https://www.npmjs.com/.
If you're familiar with node.js projects, you likely understand the funtionality behind a package.json file. In Rust, we use a Cargo.toml. This file tells Cargo which dependencies to download and information on how to build the project. Plus all these other things.
npm init
Typically to get started with a new project in node.js, you'd run npm init. In rust it is very similar, cargo init.
npm install
Similarly to npm's npm install x command to install dependencies. In Rust this is cargo add x.
And instead of npm's npm install --global to install utilities globally. In Rust it is cargo install.
Setting Up VSCode
If using Visual Studio Code, I highly recommend rust-analyzer as it provides
- code completion with imports insertion
- go to definition, implementation, type definition
- find all references, workspace symbol search, symbol renaming
- types and documentation on hover
- inlay hints for types and parameter names
- semantic syntax highlighting
- a lot of assists (code actions)
- apply suggestions from errors
... and many more, check out the manual to see them all
Conclusion
And thats it for the setup! We now have Rust installed, an understanding of cargo, and VSCode ready to write some Rust! Next time we will start by writing a Hello World program and touch on some more in-depth concepts for Rust.