Cargo
Chester Wyke June 16, 2023 Updated: April 15, 2025 #rustConditional Compilation
Install
Crate Serde
References
Cargo
Toolchain (Nightly)
String Formatting
Time
Crate Tokio
Publish Crate
rustfmt
Single file script
Snippets
CI
Pattern Type State
WASM
Create New Crate
Documentation
JSON
Pattern Builder
Testing
Working with collections of bytes
Thoughts about rust
Are we yet
OnceLock
Crate Actix Web
Stack Overflow
Crate Clap
Crate Poll Promise
Crate Insta
Tips
Create new egui project
Crate CSV
Crate egui
Iterators
Crate Tracing Subscriber
Regex
vscode
Enum Conversion
Macros
lettre
Google APIs
Configuring Cargo
See the Cargo Reference
Frequently Used Commands and arguments
Source: https://doc.rust-lang.org/cargo/commands/index.html
- doc
--document-private-items
--no-deps
--target-dir
--open
- run
--release
--bin name
(Works with multiple binaries see Additional Binaries)
- test
-- --ignored
(Works with#[ignore]
see rust reference)-- --test-threads 1
- tree
-e features
-f "{p} {f}"
-e features -i foo
Cargo Environment Variables
For Crates
To use environment variables provided by cargo use env!()
.
Example
println!;
- CARGO_PKG_NAME
- CARGO_PKG_VERSION (Prefer use of Version Crate)
Specifying dependencies
Using Git
Example
[]
= { = "https://github.com/rust-lang/regex.git", = "next" }
Adding Feature flag to a library
Source: https://doc.rust-lang.org/cargo/reference/features.html
Example of defining a feature
[]
= ["ico", "webp"]
= ["dep:ravif", "dep:rgb"]
= []
= ["dep:gif"]
= []
= ["bmp", "png"]
# Defines a feature named `webp` that does not enable any other features.
= []
Optional dependency
[]
= { = "0.11.1", = true }
= { = "0.6.3", = true }
= { = "0.8.25", = true }
Example of conditionally compiling based on feature
// This conditionally includes a module which implements WEBP support.
Mutually Exclusive Feature Flags
Source: https://doc.rust-lang.org/cargo/reference/features.html#mutually-exclusive-features
TLDR: Avoid using mutually exclusive features see source above for options to work around it otherwise detect it at compile time and provide an error message instead of waiting for duplicated functions to stop the compile.
compile_error!;
Patch a crate
Source: https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html#testing-a-bugfix
The source is sufficiently terse that I don’t find value in extracting relevant parts here other than pointing out the following:
The way [patch] works is that it’ll load the dependency at ../path/to/uuid and then whenever crates.io is queried for versions of uuid it’ll also return the local version.
This means that the version number of the local checkout is significant and will affect whether the patch is used. Our manifest declared uuid = “1.0” which means we’ll only resolve to >= 1.0.0, < 2.0.0, and Cargo’s greedy resolution algorithm also means that we’ll resolve to the maximum version within that range. Typically this doesn’t matter as the version of the git repository will already be greater or match the maximum version published on crates.io, but it’s important to keep this in mind!
Bolding applied is my own.
If you need to patch often it is worthwhile looking into cargo-override which automates overriding crates.