Empowering everyone to build reliable and efficient software
- Run a File in Rust
- Build a Project in Rust
- Run Tests in Rust
- Benchmarking in Rust
- Generating Crate Documentation Locally
- Recommended VS Code Extensions
- Learning Resources
Multiple concurrent Rust toolchains can be installed and managed via rustup. Rust installations come with Cargo, a command line tool to manage dependencies, run tests, generate documentation, and more. Rust was built with safety, speed, and efficiency (productivity and control).
-
Compile time memory checks, guuarantees memory safety with no run time costs through ownership and borrowing, can opt in to runtime garbage collection through reference counters(variables immutable by default)
-
Catching bugs at compile time and ridiculously helpful compiler errors
-
Best of two worlds from systems programming and functional/higher-order programming: Low level control and abstraction when required Zero-cost abstractions (Fluent Builder Pattern)
-
Control over memory access, memory layout and specific CPU instructions, for example, semantics for creating integers:
// An integer on the stack
let a = 10;
// Boxed integer: An integer on the heap
let b = Box::new(20);
// A boxed integer wrapped within a reference counter
let c = Rc::new(Box::new(30));
// An integer protected by a mutual exlusion lock, wrapped in an atomic reference counter
let d = Arc::new(Mutex::new(40));
- Reliable language: Code written will always compile with a future Rust compiler
Rust is slower at compiling code than its peer languages, not free from logic errors.
rustup
manages Rust toolchains and move between versions of the compiler.
cargo
manages projects.
Make the file:
touch [file-name].rs
Compile the file:
rustc [file-name].rs
Run compiled binary:
./[file-name]
-
Create a package
To initialise in a folder (
--vcs=none
flag overrides cargo default of initialising a new Git repository along with a .gitignore file):cargo new --vcs=none [folder-name]
To initialise in current folder:
cargo init
src/main.rs
is the crate root of a binary cratesrc/lib.rs
is the crate root of a library crate (to create a new library, add '--lib' flag to 'cargo new')
both with the same name as the package. A package can have multiple binary crates by placing files in the src/bin directory: each file will be a separate binary crate.
-
Compile and run
cargo run
The executable is located in
./target/debug
To build only run the following command:
cargo build
To build for production:
cargo build --release
Use Compiler Driven Development:
cargo check
See Generated Documentation:
cargo doc --open
cargo test
compiles your code in test mode and runs the resulting test binary. Tests are run in parallel by default:
cargo test
Run tests consecutively using --test-threads
flag:
cargo test -- --test-threads=1
Any arguments appearing after -- are sent through to the result of the build.
Show printed values from the tested functions regardless of success or failure:
cargo test -- --show-output
or
cargo test -- --nocapture
Rust test programs hide the stdout of successful tests in order for the test output to be tidy.
Run #[ignore]
tests:
cargo test -- --ignored
Run all tests including #[ignore]
:
cargo test -- --include-ignored
Run a particular integration test crate in tests/
:
cargo test --test test_file_name
Install Criterion as dependency
Read about Cargo Modules
Read about cfg marcos
For a single benchmark file:
cargo bench
For multiple benchmark files:
cargo bench --bench [benchmark name]
rustdoc file.rs
This creates a doc/
directory with subdirectory containing index.html.
### Use cargo
to render docs for a crate and its dependencies
Generating HTML documentation:
cargo doc
This crates index.html in /target/doc/
. To open doc in web browser:
cargo doc --open
To inspect the output directory:
tree -d -L 1 target/doc/
- Rust Analyzer
- Better TOML
- The Rust Programming Language Book
- Rust Crash Course
- The Rustonomicon
- The Rust Reference
- The Little Book of Rust Macros
- WebAssembly
- Rustlings - Small exercises
- The Edition Guide
- Michtom School of Computer Science Rust Book
To improve the throughput of a web server:
- Thread Pool
- Fork/Join model
- Single-threaded async I/O model