David Dong

David Dong

Java/C/C#/Python

Java/C/C#/Python

POST

Rust startup - environment setup

Rust is hot, I’ve heard a lot of people talking about the Rust language, and it’s powered me enough to learn more about it!

This is the first note to document the process of setting up the development environment, mainly choosing to develop under Linux.

Environments

  • OS - ubuntu 16.04
  • C Compiler - x86_64-unknown-linux-gnu
  • IDE - VS Code

Setup

The rust development environment consists of rustup (toolchain management tool), cargo (package manager and build system) and rustc (compiler).

$ export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
$ export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
$ sudo curl https://sh.rustup.rs -sSf | sh

choose option 1

& david @ david-VirtualBox in ~ 0 [15:51:08]
~ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh                                                                 0 [15:51:08]
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:

  /home/david/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/david/.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:  /home/david/.cargo/bin 
This path will then be added to your PATH environment variable by modifying the 
profile files located at:  /home/david/.profile  /home/david/.bashrc 
You can uninstall at any time with rustup self uninstall and these changes will 
be reverted. 
Current installation options:   default 
host triple: x86_64-unknown-linux-gnu     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 x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' 712.1 KiB
info: latest update on 2024-02-08, 
rust version 1.76.0 (07dca489a 2024-02-04)
info: downloading component 'cargo' 8.5 MiB / 8.5 MiB (100 %) 80.0 KiB/s in 1m 44s
info: downloading component 'clippy'  2.1 MiB /   2.1 MiB (100 %)  95.0 KiB/s in 23s
info: downloading component 'rust-docs' 14.7 MiB /  14.7 MiB (100 %) 117.4 KiB/s in 2m 
info: downloading component 'rust-std' 23.9 MiB /  23.9 MiB (100 %)  98.2 KiB/s in 4m 
info: downloading component 'rustc' 62.3 MiB /  62.3 MiB (100 %) 135.4 KiB/s in 12m 
info: downloading component 'rustfmt'  2.3 MiB /   2.3 MiB (100 %)  86.2 KiB/s in 30s 
info: installing component 'cargo'
info: installing component 'clippy'info: installing component 'rust-docs' 14.7 MiB 
info: installing component 'rust-std' 23.9 MiB /  23.9 MiB (100 %)  13.2 MiB/s in 1s
info: installing component 'rustc' 62.3 MiB /  62.3 MiB (100 %)  14.9 MiB/s in  4s 
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'  
stable-x86_64-unknown-linux-gnu installed 
- rustc 1.76.0 (07dca489a 2024-02-04)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"

& david @ david-VirtualBox in ~ 0 [16:20:53]
  • The Rustup metadata and toolchain will be installed in the Rustup home directory located at /home/david/.rustup, which can be modified using the RUSTUP_HOME environment variable.

  • Cargo’s home directory is located at /home/david/.cargo which can be modified via the CARGO_HOME environment variable.

  • cargo, rustc, rustup and other commands will be added to cargo’s bin directory, which is located in /home/david/.cargo/bin

  • add this path to the PATH environment variable /home/david/.profile or /home/david/. bashrc

    export PATH="$HOME/.cargo/bin:$PATH
    
  • I used fish shell, so need to add environment variable PATH in ~/.config/fish/config.fish

    set -x PATH ~/.cargo/bin $PATH
    
  • Verify the installation

    > rustc -V
    rustc 1.76.0 (07dca489a 2024-02-04)
    > cargo -V
    cargo 1.76.0 (c84b36747 2024-01-18)
    > rustup -V
    rustup 1.26.0 (5af9b9484 2023-04-05)
    info: This is the version for the rustup toolchain manager, not the rustc compiler.
    info: The currently active `rustc` version is `rustc 1.76.0 (07dca489a 2024-02-04)`
    

The rust toolchain management tool rustup, the package manager cargo, and the compiler rustc are now installed!

  • unstallation command

    rustup self uninstall
    

Vscode configuration for development of Rust

Install the following plug-ins

  • rust-analyzer
  • CodeLLDB
  • Code Runner

Vscode debugging Rust code

  • Create a project directory like first_prj

  • Go to first_prj

  • Run cargo new first_prj_name in terminal, it will create project first_prj_name directory and src/main. rs

    Welcome to fish, the friendly interactive shell
    Type help for instructions on how to use fish
      
    & david @ ubuntu in ~/code/rust/first_proj 0 [19:06:14]
    ~ cargo new greeting
         Created binary (application) `greeting` package
    
  • cd . /first_prj_name

  • run cargo build to compile the code

    & david @ ubuntu in ~/code/rust/first_proj/greeting (master 3) 0 [19:07:15]
    ~ cargo build
       Compiling greeting v0.1.0 (/home/david/code/rust/first_proj/greeting)
        Finished dev [unoptimized + debuginfo] target(s) in 0.18s 0.
    
  • run cargo run to debug the code

    & david @ ubuntu in ~/code/rust/first_proj/greeting (master 4) 0 [19:07:19]
    ~ cargo run
        Finished dev [unoptimized + debuginfo] target(s) in 0.00s
         Running `target/debug/greeting`
    Hello, world!
    

Rust

You may also like

further reading