Developer Tools

Rust Function Generator

Describe a function and get idiomatic Rust: parameters that borrow instead of taking ownership, Option for missing values, Result with a custom error enum, a boxed error or a String, documentation with # Errors and # Examples sections, #[must_use] for pure functions and a #[cfg(test)] module with a first unit test.

  • Runs in your browser
  • No sign-up
  • Free to use
Start from an example

int = i64, float = f64; type? = Option.

Types: string, int, float, bool, date, any, a type name, T[] (Vec) or map<K, V> (HashMap). name? = Option.

Options

    How to use Rust Function Generator

    1. Enter the function name, return type and error handling.
    2. List the parameters as name: type.
    3. Choose borrowing, documentation and test options.
    4. Copy the code into your crate.

    Rust Function Generator features

    Borrowed parameters

    &str and &[T] instead of String and Vec<T>.

    Error handling

    Custom error enum, Box<dyn Error> or String.

    Error enum

    Display and std::error::Error implementations included.

    Rustdoc

    # Errors and # Examples sections, ready for doc tests.

    Lints

    #[must_use] for functions whose result should not be ignored.

    Tests

    A #[cfg(test)] module with a first test.

    When to use Rust Function Generator

    • Starting new library functions with proper error types.
    • Learning ownership-friendly function signatures.
    • Writing documented public APIs for crates.
    • Scaffolding parsers and validators.

    Rust Function Generator FAQ

    Why borrow &str instead of taking String?

    A &str parameter accepts string literals, &String and slices without copying, and leaves ownership with the caller. Take String only when the function needs to keep the value.

    Which error type should I use?

    Libraries usually define an error enum so callers can match on the cause. Applications often use Box<dyn Error> or anyhow for convenience. String errors are simple but hard to handle programmatically.

    What does #[must_use] do?

    The compiler warns when a caller ignores the return value. Result already has this attribute, so it is added only for other return types.

    Why “let _ = (…)” in the body?

    It silences unused-parameter warnings until you implement the function; remove it when the parameters are used.

    What about default arguments?

    Rust has no default arguments. Use Option parameters, a builder or a configuration struct that implements Default.

    Is anything uploaded?

    No. The code is generated in your browser.

    Idiomatic Rust function signatures

    A Rust function signature says a lot: who owns each argument, whether the function can fail, and what it returns when there is nothing to return. Getting it right from the start avoids clones and awkward conversions at every call site. The generator applies the common idioms to a function you describe.

    Parameters use neutral types that map to Rust: String, i64, f64, bool, SystemTime, Vec, HashMap and Option for optional values. With borrowing on, string and list parameters become &str and &[T], so callers can pass literals, owned values or slices without giving up ownership, and other non-trivial types are passed by reference.

    Fallible functions return Result. A custom error enum is the most expressive choice: the generator writes an enum with Empty and Invalid variants, derives Debug, Clone and PartialEq, implements Display for readable messages and std::error::Error so that the type works with the ? operator and with crates such as anyhow. For applications, Box<dyn std::error::Error> is a quick alternative.

    Documentation comments use the sections that rustdoc and Clippy expect. # Errors explains when an error is returned, which Clippy’s missing_errors_doc lint asks for, and # Examples contains a code block that cargo test runs as a doc test. #[must_use] is added to functions whose only purpose is their return value.

    Finally a #[cfg(test)] module with use super::* and a first test is added, so cargo test runs immediately. The function body contains a TODO, a placeholder that compiles and a statement that silences unused-variable warnings until the implementation is written.

    Other useful tools