Rust
Aboutβ
Rust is a multi-paradigm programming language designed for performance and safety, especially safe concurrency. Rust is syntactically similar to C++, but can guarantee memory safety by using a borrow checker to validate references. Rust achieves memory safety without garbage collection, and reference counting is optional. (Wikipedia, 2020)
Getting startedβ
- Follow https://www.rust-lang.org/learn/get-started
- Use IntelliJ IDEA from JetBrains with https://intellij-rust.github.io/ plugin
Rustup: the Rust installer and version management toolβ
The primary way that folks install Rust is through a tool called Rustup, which is a Rust installer and version management tool.
- Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- Update Rust
rustup update
Cargo: the Rust build tool and package managerβ
When you install Rustup youβll also get the latest stable version of the Rust build tool and package manager, also known as Cargo. Cargo does lots of things:
- build your project with
cargo build
- run your project with
cargo run
- test your project with
cargo test
- build documentation for your project with
cargo doc
- publish a library to crates.io with
cargo publish
To test that you have Rust and Cargo installed, you can run this in your terminal of choice:
cargo --version
Create a new projectβ
cargo new my_project
Clippy: the Rust linterβ
A collection of lints to catch common mistakes and improve your Rust code.
- https://doc.rust-lang.org/stable/book/appendix-04-useful-development-tools.html?highlight=clippy#more-lints-with-clippy
- To run Clippyβs lints on any Cargo project, enter the following
cargo clippy
How to setup a workspaceβ
How to work with Rust in IntelliJ IDEAβ
Debugging with Macro std::dbg
β
Prints and returns the value of a given expression for quick and dirty debugging.
Use dbg!(..)
see https://doc.rust-lang.org/std/macro.dbg.html
let a = 2;
let b = dbg!(a * 2) + 1;
// ^-- prints: [src/main.rs:2] a * 2 = 4
assert_eq!(b, 5);
Resourcesβ
Compile error?β
If you are facing an error like that on new MacOS version.
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
It means that you need to install XCode command line, open a Terminal and run this command:
xcode-select --install
Note: If you want to download and install Command Line tools manually, it can be downloaded from: https://developer.apple.com/download/more/