Rust docs
General info
rustup
- command line tool for managing Rust versions and associated tools (updater)rustc
- compilercargo
- package manager to download dependencies and the frontend to the compilerrustfmt
- to format rust codeclippy
- linter to analyze source code for bugs
Setup
Install
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
Installs rustup
, rustc
, cargo
, rustfmt
, clippy
. You will also need a linker, which you can get from C compiler.
Pass these env variables as per your need
RUSTUP_HOME
(~/.rustup
) - where rustup will store metadataCARGO_HOME
(~/.cargo
)cargo
,rustc
,rustup
- these commands are located at~/.cargo/bin
and would be added to PATH variables automatically during install
Use rustc --version
to verify install (after restarting the shell).
Update
rustup update
Uninstall
rustup self uninstall
VSCode
Use the rust-analyzer LSP. Install it using ext install rust-lang.rust-analyzer
.
editor.inlayHints.enabled
- show inferred types, return values, named parameters in light text- Hovering on variable, function, type, or keyword will give you info on that item including documentation, signature.
editor.semanticTokenColorCustomizations
- semantic syntax highlighting. Mutable variables are underlined.rust-analyzer.check.command
- set toclippy
to usecargo clippy
when you save a file and display clippy errors and warning directly in the editor.
Vscode shortcuts
- Auto completions (can be triggered manually using
Ctrl-Space
). - To manually show quick fixes use
Ctrl + .
. - Change a variable name by hovering over the name and press
F2
(or select Rename Symbol). - Use
Ctrl-Shift-I
to format document manually or useeditor.formatOnSave
andeditor.formatOnPaste
.
Install CodeLLDB
extension for debugging.
- select
Debug: Allow Breakpoints Everywhere
- Use
Rust Analyzer: Debug
or select theDebug
Codelens.
Clippy
cargo clippy
- Run clippy.
cargo clippy --fix
- automatically apply lint suggestions (--fix
implies --all-targets
).
cargo clippy -- -D warnings
- build will fail when encountering warnings.
Add clippy to CI
language: rust
rust:
- stable
- beta
before_script:
- rustup component add clippy
script:
- cargo clippy
# if you want the build job to fail when encountering warnings, use
- cargo clippy -- -D warnings
# in order to also check tests and non-default crate features, use
- cargo clippy --all-targets --all-features -- -D warnings
- cargo test
# etc.
rustfmt
cargo fmt
- run the formatter for the given directory
rustfmt filename
- format a single file
Create rustfmt.toml
or .rustfmt.toml
to configure the rustfmt.
Add rustfmt to CI
language: rust
before_script:
- rustup component add rustfmt
script:
- cargo build
- cargo test
- cargo fmt --all -- --check
Hello, World
touch main.rs
Add the code
fn main() {
println!("Hello, world!");
}
rustc main.rs
./main
Hello, Cargo
Cargo is used to build your code, download dependencies, and build those dependencies (libraries).
cargo new project_name
- initializes a git repo also. But if already inside a git repo, it does not.
cargo new project_name vcs=none
- to initialize a project without git.