Skip to content

Types, APIs and errors

RCS-TYPE-001 — Model consequential identities distinctly

Level: Required when accidentally interchanging same-primitive values would materially affect correctness or safety.

Enforcement: Compiler type checking, focused tests and human review.

Do not pass logically distinct identities interchangeably as loose strings or integers when their confusion could change the operation's target.

Avoid:

fn bind(source_id: &str, target_id: &str) {
    // Both arguments are strings: swapping them still type-checks.
}

bind("target-17", "source-42");

Prefer:

#[derive(Clone, Debug, Eq, PartialEq)]
struct SourceId(String);

#[derive(Clone, Debug, Eq, PartialEq)]
struct TargetId(String);

fn bind(source_id: &SourceId, target_id: &TargetId) {
    // The compiler distinguishes source and target identities.
}

The example is intentionally small. Real constructors should validate externally supplied IDs. A newtype is not mandatory for every internal counter, temporary string or obvious local value: use it where the type carries a meaningful invariant.

RCS-API-001 — Follow predictable API conventions

Level: Advisory for public or broadly reused Rust APIs.

Enforcement: Review, Rustdoc and relevant Clippy lints.

  • Choose clear constructors (often new) and use conventional trait implementations when they make behavior unambiguous.
  • Prefer borrowing when ownership transfer is not needed; avoid repeated allocations merely to satisfy a convenient signature.
  • Expose the narrowest useful public surface rather than exporting internal state or implementation details.
  • Use intuitive getters and method names. If an operation is fallible, expose that fact in its return type.
  • Document non-obvious invariants and resource lifetimes where users need them.

For read-only input, prefer the view the operation actually needs: &str rather than &String, &[T] rather than &Vec<T>, or &Path rather than &PathBuf. These signatures accept more existing storage types without forcing callers to construct a particular container. They do not imply that borrowing a String or Vec itself allocates. For example:

fn has_selected_label(labels: &[String], wanted: &str) -> bool {
    labels.iter().any(|label| label.as_str() == wanted)
}

For an application-only API, do not introduce generic impl Into<String> or impl IntoIterator parameters for hypothetical callers when a plain borrowed signature is clearer. Keep ownership explicit when a callee must retain, mutate, transfer, or outlive the source; borrowing does not replace current identity checks on mutable resources.

A Boolean is fine when its purpose is obvious. If an opaque Boolean chooses a consequential operation (such as preview versus apply), a named mode makes a call site harder to misread.

Avoid introducing wrapper types and traits for hypothetical future integrations without a concrete consumer. Consult the Rust API Guidelines, but apply public-library advice proportionally to private application code.

RCS-ERR-001 — Use explicit recoverable errors

Level: Required on a path that can fail because of I/O, malformed input, provider output, or other expected environmental conditions.

Enforcement: Code review, Clippy and negative-path tests.

Prefer Result with ordinary ? propagation instead of hiding errors, unconditionally panicking on untrusted inputs or returning guessed success values.

Preferred:

use std::{fs::File, io::{self, Read}, path::Path};

fn read_magic(path: &Path) -> io::Result<[u8; 4]> {
    let mut file = File::open(path)?;
    let mut magic = [0_u8; 4];
    file.read_exact(&mut magic)?;
    Ok(magic)
}

A failed I/O read must not produce an invented digest or a partially trusted state. When an Option or Result branch is already being checked, prefer if let, match, or let ... else to a predicate followed by .unwrap(); the matched value makes the successful branch and failure path explicit. expect/unwrap remain reasonable in tests, examples and contexts with proven invariants; a blanket ban is not intended.

RCS-ERR-002 — Preserve actionable boundary context

Level: Advisory.

Enforcement: Human review and error-path testing.

Errors should identify the failed operation class or contract at the point where callers can act on it. Avoid error enums that collapse all failures into an unhelpful boolean or indiscriminately convert dangerous ambiguity into "not found."

Prefer typed errors and meaningful context where it improves recovery. Do not build a deep error-wrapper hierarchy if simple standard errors remain expressive.

Back to standards index.