/ RUST

Rust language - Basic & Tips

Installation

Installing rustup on Linux or macOS

If you’re using Linux or macOS, open a terminal and enter the following command:
$ curl https://sh.rustup.rs -sSf | sh

The installation script automatically adds Rust to your system PATH after your next login.
If you want to start using Rust right away instead of restarting your terminal, run the following command in your shell to add Rust to your system PATH manually:
$ source $HOME/.cargo/env

Updating and Uninstalling

After you’ve installed Rust via rustup, updating to the latest version is easy. From your shell, run the following update script:
$ rustup update
To uninstall Rust and rustup, run the following uninstall script from your shell:
$ rustup self uninstall

Check Rust version

To check whether you have Rust installed correctly:
$ rustc --version

Local documentation

The installer also includes a copy of the documentation locally, so you can read it offline.
Run rustup doc to open the local documentation in your browser.
$ rustup doc

Cargo

Cargo is Rust’s build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries. (We call libraries your code needs /dependencies/.)

Check whether Cargo:

$ cargo —version

Creating a new project:

$ cargo new hello_cargo
$ cd hello_cargo

Building and Running a Cargo Project

From your /hello_cargo/ directory, build your project by entering the following command:
$ cargo build

This command creates an executable file in /target/debug/hello_cargo/ rather than in your current directory. You can run the executable with this command:
$ ./target/debug/hello_cargo

We just built a project with cargo build and ran it with ./target/debug/hello_cargo, but we can also use cargo run to compile the code and then run the resulting executable all in one command:
$ cargo run

Cargo also provides a command called cargo check.
This command quickly checks your code to make sure it compiles but doesn’t produce an executable:
$ cargo check

Rustup and the Role of Rust Nightly

Rustup makes it easy to change between different release channels of Rust, on a global or per-project basis. By default, you’ll have stable Rust installed.
To install nightly, for example:
$ rustup install nightly

You can see all of the /toolchains/ (releases of Rust and associated components) you have installed with rustup as well:
$ rustup toolchain list

As you can see, the stable toolchain is the default. Most Rust users use stable most of the time. You might want to use stable most of the time, but use nightly on a specific project, because you care about a cutting-edge feature.
To do so, you can use rustup override in that project’s directory to set the nightly toolchain as the one rustup should use when you’re in that directory:

$ cd ~/projects/needs-nightly
$ rustup override set nightly

Now, every time you call rustc or cargo inside of /~/projects/needs-nightly/, rustup will make sure that you are using nightly Rust, rather than your default of stable Rust. This comes in handy when you have a lot of Rust projects!

Tips

Compiling errors & possible fixes

Output message:

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-m64" "-L" "/Users/<username>/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib"

or

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun.

Run the following line on terminal:

xcode-select --install

This will download and install xcode developer tools and should fix the problem.

Rust language - Basic & Tips
Share this

Subscribe to NL Slack