Guidelines and recommendations we use at Embark for developing our Rust 🦀 projects.
This is not meant to be an exhaustive collection and resource of all best practices in Rust, that is best covered by the Rust Book and other references. Instead this is list of specific guidelines and recommendations that we've run into and want to highlight and standardize on within Embark.
Motivation: We want to keep a consistent code base where we don't have to argue about style, so we use rustfmt on everything and verify on CI that it has been run. So highly recommend everyone to configure their editor to run rustfmt on save as it is more error prone to run it before commit.
With VS Code this should be automatic for our repos, see section.
002 - Prefer synchronization primitives from parking_lot
instead of std::sync
Motivation: The parking_lot
implementations are smaller, faster, and avoids poisioning errors which makes them more ergonomic and easier to use.
Example old:
use std::sync::{Mutex,RwLock};
let mutex = Mutex::new(1u32);
*mutex.lock().unwrap() += 5;
Example new:
use parking_lot::{Mutex,RwLock};
let mutex = Mutex::new(1u32);
*mutex.lock() += 2;
Todo: Would be great to automatically verify this through something like a custom clippy lint, or even better: have the parking_lot
primitives eventually find their way into std.
Motivation: Keeps our code high quality and catches common errors and inefficiencies. We already verify on CI that clippy finds no warnings, and by opting in to warnings in every crate it also enables tools like RLS in VS Code to know that Clippy warnings
Add this to the top of the main file (lib.rs
or bin.rs
) for all of our crates:
#![warn(clippy::all)]
#![warn(rust_2018_idioms)]
Todo: Would be great if we could configure this globally for our workspace instead of for each crate (more error prone). #22.
We primarily use VS Code as development environment for Rust.
Extensions:
- Main:
- Rust analyzer - language support for Rust
- Crates - easy Cargo.toml dependency version management
- C/C++ - for debugging on Windows
- Native Debug - for debugging on Linux & Mac using GDB/LLDB
- Other:
- GitHub
- GitHub Pull Requests
- GitLens
- Better TOML
- markdownlint
- Markdown Preview Github Styling
- Shader languages support for VS Code
- TODO Highlight
With VS Code this should be automatic for our repos, see as long as one has the Rust extension installed and our repo contains a .vscode/settings.json
with:
As we want to run rustfmt on save, all of our repos should have a .vscode/settings.json
with:
{
"editor.formatOnSave": true,
}