docs/RUST_STYLE.md
**Note: ** This is a draft document primarily intended for AI agents (like Claude) to understand Vector's Rust coding conventions. These guidelines help ensure consistent code generation and modifications.
This document outlines Rust coding conventions and patterns for Vector development.
use)All use statements must be at the top of the file/module or at the top of mod tests.
This is for consistency.
Correct:
use std::time::Duration;
use governor::clock;
use crate::config::TransformConfig;
fn my_function() {
// function code
}
Incorrect:
fn my_function() {
use std::time::Duration; // WRONG; Do not insert `use` inside functions
// function code
}
Organization:
std → external crates → internal (crate::)rustfmt to automatically organize them: make fmtAlways use the Tracing crate's key/value style:
Correct:
warn!(message = "Failed to merge value.", %error);
info!(message = "Processing batch.", batch_size, internal_log_rate_secs = 1);
Incorrect:
warn!("Failed to merge value: {}.", err); // Don't do this
Rules:
error (not e or err) for error values%error not ?errorPrefer inline variable syntax in format strings (Rust 1.58+).
Correct:
format!("Error: {err}");
println!("Processing {count} items");
Incorrect:
format!("Error: {}", err); // Unnecessary positional argument
println!("Processing {} items", count);
Why: Inline syntax is more readable and reduces mistakes with argument ordering.
Code in Vector should NOT panic under normal circumstances.
Result<T, E> and proper error handlingNew components (sources, sinks, transforms) must be behind feature flags:
# Build only specific component for faster iteration
cargo test --lib --no-default-features --features sinks-console sinks::console
See features section in Cargo.toml for examples.
Cargo features are additive: once any crate in the dependency graph enables a feature, it is enabled for the entire build. This can be surprising — enabling a feature in one crate silently turns it on everywhere.
Always set default-features = false in [workspace.dependencies]. For feature declarations, the rule is simple:
Cargo.toml.[workspace.dependencies].Add a short comment near the dependency when the feature setup is non-obvious.
Auditing: when unsure whether a feature is truly needed or only transitively enabled, verify with:
# Show the full feature tree for the workspace
cargo tree -e features
# Narrow down to a specific dependency
cargo tree -e features -i <crate-name>
The -i flag shows which crates depend on <crate-name> and which features they activate, useful for tracing where a feature is coming from. If a feature only appears because another crate enables it, and your crate relies on that feature being present, declare it explicitly — otherwise your crate silently breaks if the other crate ever stops enabling it.