Environment Setup
To get a more developer-like experience, you'd need to install rust toolchain on your machine.
Install Rustup toolchain installer, the site suggests the following command for linux
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
For windows check rust in windows you'd need also need to install Vistual Studio build tools.
A toolchain is the combination of a compilation target (converting rust code to machine code for a platform) and a release channel (release version such as stable, beta, nightly). Target platforms are split into tiers, from “guaranteed-to-work” Tier 1 to “best-effort” Tier 3.
rustup toolchain list will give you an overview of what toolchain components are installed on your system.
This rustup tool that can manage multiple versions of Rust compiler on a machine. To check installation and to update the compiler version run: rustup update
If you want to develop inside a dev container, you can create one by following through this tutorial. In short install and configure Docker in your system, install "Dev Container" extension in VSCode and add and edit a dev container configuration file to the repo and rebuild the container to get started.
Alternatively you can try code in Online Rust Playground
IDEs and Tools
There are different IDE available for rust development. Visual Studio Code is the most popular one.
To setup for rust install rust-analyzer is an implementaion of Language server Protocol for VScode. It will provide you with code completion, syntax highlighting, and other features. The toolset also includes linting provided by rustc and clippy to detect issues with your code.
If you are using vim you will have to install rust-analyzer from rustup and rust.vim
Jetbrain also provide a separate IDE RustRover
Cargo : Rust build tool, dependency manager and package manager
rustc is the Rust compiler that allows us to compile the file, but managing multiple files and dependencies can be difficult hence we use Cargo.
-
Start a project :
cargo new prj_namecreates a folder with
Cargo.tomlmanifest files to track metadata and dependencies andsrc/main.rswith hello world script- initialize with a new git repo as well
cargo new --vcs=git prj - creating a binary executable project :
cargo new prj_name --bin - creating a library project
cargo new prj_name --lib--> createslib.rsinstead ofmain.rsas the entry point.
- initialize with a new git repo as well
-
Quickly check code to make sure it compiles without producing an executable
cargo check -
cargo add crate_name: This will add the latest crate from the package registry Crates.io to theCargo.tomlfile. To use the library you'll have to import the page in your source code:use crate_name::func;All crates on crates.io will automatically have its documentation built and available on docs.rs
eg:
cargo add randTo use dependencies in the code add the following line#![allow(unused)] fn main() { use rand::Rng; let rand_num = rand::thread_rng().gen_range(1..=100); }- use
--git URL --branch branch --tag tagto add crate from git URL branch and tag - use
--devadd as dev dependency,--buildto add as build dependency,--targetadd as target dependency to the given target platform - use
-F featuresor--feature featuresfeature to add/activate additional features available in the crate
-
Build your project:
cargo buildcompiles into a target/debug folder with compiled file and other build files
- binaries can also be generate using
rustc main.rs, but it becomes easier to build with cargo with multiple files and dependencies - building for release compiles with optimization to target/release
cargo build --release - build also creates cargo.lock file that figure out and stores the versions of dependencies that fits the criteria and ensures we rebuild the same artifact every time
- binaries can also be generate using
-
cargo updatewill ignore the Cargo.lock file and update a crate to newer version that fit the specification in Cargo.toml -
Compile and Run the project:
cargo run -
Test your project:
cargo test -
Build documentation for your project:
cargo doc --openalso opens the doc in a browserbuilds into
target\doccompiling doc comments///from code into documentation -
Remove generated artifacts:
cargo cleanremoves
/targetfolder -
Publish a library to crates.io:
cargo publish
Cargo is extensible with sub command plugins such as:
- Linter: Clippy: install using
rustup component add clippyand run (and automatically fix )usingcargo clippy --fix - Formatter: Rustfmt: auto-formatting to community standards, run using
cargo fmt
Other advance features: workspaces, build scripting.