For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /packs/languages/rust/functions.md.
Quick start

Functions

Make required ownership and failure behavior visible in the signature.

Contracts

Why

A borrowed string communicates that reading it does not require ownership of its allocation.

Rules

  • MUST use named fn declarations for reusable operations. [review]
  • MUST annotate public return types and express expected failure with Result. [review]
  • MUST accept &str and slices when ownership is not required. [review]
  • MUST take owned values only when the operation retains or consumes them. [review]
  • MUST use a named input struct when several parameters form one domain request. [review]
  • MUST keep each operation focused on one responsibility. [review]
  • MAY use closures as iterator callbacks or injected capabilities. [review]

Example

pub fn total_lengths(names: &[&str]) -> usize {
    names.iter().map(|name| name.len()).sum()
}

#[test]
fn counts_borrowed_names() {
    assert_eq!(total_lengths(&["docs", "code"]), 8);
}

[review]

The compiler and tests check signature use. Review checks ownership decisions and operation scope.

Enforcement

The compiler and tests check signature use. Review checks ownership decisions and operation scope.