Rust Note 001 Setup Your Work Environment
Install Rust toolchain
the main tool to download and update the Rust toolchain is called
rustup
For Windows
download rustup-init.exe and execute it
For Linux
execute the following command from a shell
If you don’t like to send unknown scripts in your shell, you can download for your platform from here
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --help
Compilation Targets
Rust runs on a very large group of platforms, including “embedded” chipsets, i.e. without an operating system.
The toolchain is capable of doing some cross-platform compilation if paired with the correct c compiler and linker – but this topic is too complex for this post
the paltform triple include:
- the stability of the toolchain (stable, nightly)
- the hardware architecture (arm, intel 64 bit, …)
- the os
- the ABI backend when necessary (like the three musketeers who where actually four)
stability | hardware | os | ABI | target identifier |
---|---|---|---|---|
stable | 64-bit Intel | Windows | Microsoft VS | x86_64-pc-windows-msvc |
stable | 64-bit Intel | Windows | MingW GCC | x86_64-pc-windows-gnu |
stable | 64-bit Intel | Linux | GCC | x86_64-unknown-linux-gnu |
stable | 64-bit ARM | Linux | GCC | aarch64-unknown-linux-gnu |
Cargo: Package and project manager
the toolchain is composed of several pieces; we already mentioned
rustup
(which will be useful also to keep your rust installation updated) but we must get to know
rustc
: the actual rust compiler andcargo
: project / package / dependencies manager
rustc
, cargo
is a really convenient command line application which allows to:
- set up define and manage projects
- identify and download all necessary packages
- define different compilation profiles
- also cross-compile for different platform.
module
: each file can define a module; one or more modules are usually grouped into packages which offer a coherent functionalities; these are called crates
Photo by Kelli McClintock on Unsplash
cargo
uses a TOML formatted configuration file in the root directory of the project, which is called Cargo.toml
Check and run test program
Let’s use
this will create all basic directories and files under a newly created ’hello’ directory; by default this will be generate a single binary
Under the
which does exactly what you think.
You can compile and execute it with the following command
cargo
to define our first project:
cargo init hello
src
directory we can find our main entry point (main.rs
)
fn main() { println!("Hello, world!"); }
cargo run
Prepare IDE (opinionated)
Install VSCode
for every platform:
For Windows
For Linux
you can either download it or use your distribution’s package manager e.g.
pacman -S vscode
Install extensions
rust-analyzer
: (site) as of today (October 15th, 2023) this is the best Language Server available for Rust- provides code highlighting, completion and type hints
Better TOML
: useful to edit files likeCargo.toml
which include all details about your projectCodeLLDB
: debuggerCrates
: allows you get the available versions of each crate just hovering on the dependencies secion of yourCargo.toml
Error Lens
: help read error in lines easily
The opinionated part
this installation setup suggestion is one of the best advice I found for new programmers.
I have to admit that my favrite tool for the job is Emacs, but I will add some configuration setup hint here as soon as I’m able to have the same level of features described above.