Loading...
Loading...
Rust coding standards. Use when writing Rust code. Covers edition, error handling with thiserror/anyhow, unsafe policy, type patterns, testing, documentation, and dependency management.
npx skill4agent add lgtm-hq/ai-skills stand-rusteditionCargo.tomllintrustfmtclippycargo_auditcargo_deny-D warningsrustfmt.tomlthiserroranyhow.unwrap()?Result.expect()// Good
let config = load_config().expect("config.toml must exist at startup");
// Bad
let config = load_config().unwrap();std::fmt::Display.unwrap_or_default().unwrap_or()// Don't
let count = match maybe_count {
Some(n) => n,
None => 0,
};
// Do
let count = maybe_count.unwrap_or(0);struct UserId(u64)u64impl TraitDebugClonePartialEqEqHash&strStringStringunsafe// SAFETY:// SAFETY: pointer is guaranteed non-null by the allocator contract,
// and the lifetime is bounded by the enclosing scope.
unsafe { ptr.as_ref() }std::sync::Mutex////// Parse a duration string like "5s", "100ms", or "2m".
///
/// # Examples
///
/// ```
/// use mycrate::parse_duration;
///
/// let d = parse_duration("5s").unwrap();
/// assert_eq!(d, std::time::Duration::from_secs(5));
/// ```
pub fn parse_duration(s: &str) -> Result<Duration> { ... }#![deny(missing_docs)]//!#[cfg(test)] mod teststests/#[should_panic(expected = "...")]proptestquickcheckassert_eq!assert_ne!assert!#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_duration_seconds() {
let d = parse_duration("5s").unwrap();
assert_eq!(d, Duration::from_secs(5));
}
#[test]
#[should_panic(expected = "invalid format")]
fn parse_duration_rejects_garbage() {
parse_duration("not_a_duration").unwrap();
}
}uv run lintro chkcargo_auditcargo_denyCargo.tomlimpllet-elseif let// Don't
if let Some(user) = lookup(id) {
if let Some(email) = user.email {
send(email);
}
}
// Do
let Some(user) = lookup(id) else { return };
let Some(email) = user.email else { return };
send(email);.find().position().any()// Don't
let mut idx = None;
for (i, item) in items.iter().enumerate() {
if item.id == target {
idx = Some(i);
break;
}
}
// Do
let idx = items.iter().position(|item| item.id == target);matches!()// Don't
let is_ready = match state {
State::Ready => true,
_ => false,
};
// Do
let is_ready = matches!(state, State::Ready);#[must_use]FromIntolint