OpenViSTAI · Agents · Blockchain · Crypto
Menu
OPENVIST RECIPES

Classify Unicode scalar palindromes in text lines with Rust

Rust

Identify exact reversal symmetry in scalar sequences without applying language-specific normalization.

Rust code excerpt: Classify Unicode scalar palindromes in text lines with Rust. Version 0.1.0. Read the full recipe for code, sources and recorded test scope.

Identify exact reversal symmetry in scalar sequences without applying language-specific normalization. Work only with supplied input; this does not contact external services or modify input.

Before you start

  • Save as recipe.rs and compile using Rust 1.99.0: rustc --edition=2021 recipe.rs -o recipe
  • Run recipe with no arguments, supplying input through stdin. Help flags and all other arguments are rejected.
  • Reads at most 65537 bytes and rejects input 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. Before classification, enforces at most 100 retained lines and at most 256 UTF-8 bytes per retained line.
  • Classifies retained lines in original order by comparing Unicode scalar values forward and backward. Case, spaces, punctuation, and combining characters remain unchanged; there is no trimming, normalization, grouping, deduplication, or grapheme-cluster interpretation.
  • On success, exits 0 and writes exactly one JSON object followed by a newline, with no stderr. Empty input or input consisting only of LF characters produces {"palindromes":[]}.
  • Invalid arguments, invalid input, and stdin-read failures exit 2 with no stdout and exactly error: invalid input followed by a newline on stderr, subject to the allocation and output-write limitations below. Diagnostics never include input values.
  • Uses only the standard library and permitted APIs. No Cargo, external crates, unsafe code, filesystem access, networking, subprocesses, or dynamic loading are used.

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 may block while waiting for EOF or enough bytes to reach the 65537-byte read bound.
  • Memory use is O(B + L + J), where B is at most 65537 input bytes, L is at most 100 borrowed line references, and J is the small JSON output buffer. Allocation capacities, I/O buffering, and runtime overhead may exceed logical data sizes. Classification does not allocate scalar arrays.
  • A BOM is forbidden only at the beginning of the complete input. U+FEFF elsewhere is an ordinary scalar. Only LF separates lines; other Unicode line-separator characters remain part of a line.
  • Successful output can reveal sensitive properties of the input, including the number and order of retained lines and each palindrome result. Input bytes remain in process memory until released and are not securely erased.
  • Memory-allocation failures and output-write failures are outside the input-error guarantee. A detected stdout write or flush failure exits 2 and may leave partial stdout. Stderr failures may prevent delivery of the complete error diagnostic.
  • Scalar reversal is not a linguistic or grapheme-aware palindrome test.

Run it

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

Example input

aba
abc
💻a💻

Expected output

{"palindromes":[true,false,true]}
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};
use std::str;

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

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

    if bytes.len() > 65_536 {
        return Err(());
    }

    let text = str::from_utf8(&bytes).map_err(|_| ())?;
    if text.starts_with('\u{FEFF}') || text.contains('\r') {
        return Err(());
    }

    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);
    }

    // All input validation is complete before classification begins.
    let mut output = String::from("{\"palindromes\":[");
    for (index, line) in lines.iter().enumerate() {
        if index != 0 {
            output.push(',');
        }
        let palindrome = line.chars().eq(line.chars().rev());
        output.push_str(if palindrome { "true" } else { "false" });
    }
    output.push_str("]}\n");
    Ok(output)
}

fn main() {
    match prepare_output() {
        Ok(output) => {
            let stdout = io::stdout();
            let mut writer = stdout.lock();
            if writer.write_all(output.as_bytes()).is_err()
                || writer.flush().is_err()
            {
                std::process::exit(2);
            }
        }
        Err(()) => {
            let stderr = io::stderr();
            let mut writer = stderr.lock();
            let _ = writer.write_all(b"error: invalid input\n");
            let _ = writer.flush();
            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.