Rust Note 001 Setup Your Work Environment

altumcode-mCj7UinqOYQ-unsplash-scaled.jpg
Photo by AltumCode on Unsplash

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
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --help
If you don’t like to send unknown scripts in your shell, you can download for your platform from here

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)
here are some target identifiers examples, a complete updated list is here
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 and
  • cargo : project / package / dependencies manager
venti-views-FPKnAO-CF6M-unsplash-scaled.jpg Photo by Venti Views on Unsplash While it is entirely possible to develop in rust just using 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. platform_meme.jpg
Let’s introduce some terminology here: rust minimal compilation element is called 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 kelli-mcclintock-DcoB_NoNl6U-unsplash-2.jpg 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 cargo to define our first project:
cargo init hello
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 src directory we can find our main entry point (main.rs)
fn main() {
    println!("Hello, world!");
}
which does exactly what you think. You can compile and execute it with the following command
cargo run

Prepare IDE (opinionated)

Install VSCode

for every platform:

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 analyzer_type_hint_2.png
  • Better TOML: useful to edit files like Cargo.toml which include all details about your project
  • CodeLLDB: debugger
  • Crates: allows you get the available versions of each crate just hovering on the dependencies secion of your Cargo.toml crates_2023-10-15-22-14.gif
  • Error Lens: help read error in lines easily error_lens_example.png

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.

marco.p.v.vezzoli

Self taught assembler programming at 11 on my C64 (1983). Never stopped since then -- always looking up for curious things in the software development, data science and AI. Linux and FOSS user since 1994. MSc in physics in 1996. Working in large semiconductor companies since 1997 (STM, Micron) developing analytics and full stack web infrastructures, microservices, ML solutions

You may also like...