OpenViSTAI · Agents · Blockchain · Crypto
Menu
OPENVIST RECIPES

Classify ASCII identifier spellings with Rust

Rust

Check a local identifier spelling rule independently of compiler keywords and provider naming policies.

Rust code excerpt: Classify ASCII identifier spellings with Rust. Version 0.1.0. Read the full recipe for code, sources and recorded test scope.

Check a local identifier spelling rule independently of compiler keywords and provider naming policies. Work only with supplied input; this does not contact external services or modify input.

Before you start

  • Save as recipe.rs and compile with Rust 1.99.0 using: rustc --edition=2021 recipe.rs -o recipe
  • Run recipe with no arguments and supply input through stdin. Help flags and all other arguments are rejected.
  • Reads at most 65537 bytes and rejects inputs exceeding 65536 bytes. Requires strict UTF-8, rejects a leading UTF-8 BOM, and rejects every CR, including CRLF.
  • Splits only on LF and discards exactly empty segments. Retains input order without trimming, normalization, grouping or deduplication. Before classification, enforces at most 100 nonempty lines and at most 256 UTF-8 bytes per retained line.
  • A spelling is true exactly when it matches the ASCII convention [A-Za-z_][A-Za-z0-9_]*. Keywords are accepted. Other spellings, including non-ASCII text and whitespace-bearing lines, produce false rather than an input error. Empty input produces {"valid":[]}.
  • Success exits 0 and writes exactly one JSON object followed by a newline, with no stderr. Invalid arguments, invalid input and stdin-read failures exit 2, emit no stdout, and write exactly error: invalid input followed by a newline to stderr, subject to the failure limitations below.

Build and check

  1. Save the example as recipe.rs.
  2. Compile with Rust 1.99.0 using rustc --edition=2021 recipe.rs -o recipe.
  3. Prepare UTF-8 input.txt following the documented operation-specific conventions.
  4. Run ./recipe < input.txt.
  5. Inspect the JSON result. Exit 2 with exactly error: invalid input followed by a newline on stderr and no stdout identifies rejected input or stdin-read failure. Output-write failures may also exit nonzero, without that diagnostic and potentially after partial stdout; these are outside the input-error guarantee.

Limits to understand

  • Stdin can block while waiting for more bytes or EOF. Input within the accepted size bound requires EOF before processing finishes.
  • Memory use is O(B + L + R), where B is the bounded input buffer, L is at most 100 borrowed line references, and R is the small JSON result. Buffer capacity and allocator overhead can exceed the logical input size.
  • Successful output reveals the number, order and lexical validity of retained spellings; treat it as potentially sensitive.
  • Memory-allocation failures and output-write failures are outside the input-error guarantee. A stdout write or flush failure exits 2 and may leave partial stdout. Failed stderr writes can prevent delivery of the required diagnostic.
  • This checks only the stated ASCII spelling convention, independently of compiler keyword rules and provider naming policies. It does not establish provider compatibility, production suitability or uniqueness.

Run it

rustc --edition=2021 recipe.rs -o recipe
./recipe < input.txt

Example input

name
_x2
2x
x-y

Expected output

{"valid":[true,true,false,false]}
INSPECT THE SCRIPT

Inspect, copy and run the documented checks.

This browser preview builds code locally. It does not contact an API or run the code.

Default example ready.

Rust example
use std::io::{self, Read, Write};

fn invalid_input() -> ! {
    let _ = io::stderr().lock().write_all(b"error: invalid input\n");
    std::process::exit(2);
}

fn prepare_output() -> Result<String, ()> {
    if std::env::args_os().skip(1).next().is_some() {
        return Err(());
    }

    let mut input = Vec::new();
    io::stdin()
        .lock()
        .take(65_537)
        .read_to_end(&mut input)
        .map_err(|_| ())?;

    if input.len() > 65_536 {
        return Err(());
    }
    let text = std::str::from_utf8(&input).map_err(|_| ())?;
    if text.starts_with('\u{feff}') || input.contains(&b'\r') {
        return Err(());
    }

    // Validate every retained line before classifying any spelling.
    let mut lines = Vec::new();
    for line in text.split('\n') {
        if line.is_empty() {
            continue;
        }
        if line.len() > 256 || lines.len() >= 100 {
            return Err(());
        }
        lines.push(line);
    }

    let mut output = String::from("{\"valid\":[");
    for (index, line) in lines.iter().enumerate() {
        let bytes = line.as_bytes();
        let first_ok = bytes[0].is_ascii_alphabetic() || bytes[0] == b'_';
        let rest_ok = bytes[1..]
            .iter()
            .all(|b| b.is_ascii_alphanumeric() || *b == b'_');
        if index != 0 {
            output.push(',');
        }
        output.push_str(if first_ok && rest_ok { "true" } else { "false" });
    }
    output.push_str("]}\n");
    Ok(output)
}

fn main() {
    let output = match prepare_output() {
        Ok(output) => output,
        Err(()) => invalid_input(),
    };

    let mut stdout = io::stdout().lock();
    if stdout.write_all(output.as_bytes()).is_err() || stdout.flush().is_err() {
        std::process::exit(2);
    }
}
What to verify after running
  • The example must produce the documented JSON result.
  • Empty stdin must produce the documented empty result.
  • Reject extra arguments, malformed UTF-8, a leading BOM, oversized input and unreadable stdin with the fixed input-error diagnostic and no stdout.

Request or format preview

Illustrative preview. No provider response, price or balance is fetched.

Change a setting to inspect the preview.

Official sources and related profiles

    Code license: MIT. Keep the license notice with your copy.

    Example version 0.1.0 · Documentation reviewed 2026-10-14.

    Six independent CLI fixtures and 16 supplementary cases are executed in pinned, offline Rust 1.99.0 containers. These checks do not establish exhaustive correctness or behavior on other versions.