OpenViSTAI · Agents · Blockchain · Crypto
Menu
OPENVIST RECIPES

Locate breaks in strictly increasing integer sequences with Rust

Rust

Report where a number fails to increase relative to its immediate predecessor.

Rust code excerpt: Locate breaks in strictly increasing integer sequences with Rust. Version 0.1.0. Read the full recipe for code, sources and recorded test scope.

Report where a number fails to increase relative to its immediate predecessor. 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 without 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.
  • Splits only at LF and discards exactly empty segments. Before numeric interpretation, enforces at most 100 retained lines and at most 256 UTF-8 bytes per retained line. No trimming or normalization occurs.
  • Each retained line must be canonical ASCII decimal from 0 through 1000000, with no sign, whitespace, or leading zeroes except the single digit 0.
  • Returns one-based retained-line positions starting at position 2 where the value is less than or equal to its immediate predecessor. Duplicates count as breaks; order is preserved.
  • Empty input or input containing only LF bytes returns {"positions":[]}.
  • Success exits 0 and writes exactly one JSON object plus a newline, with no stderr. 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 failure limitations below.
  • Uses only the standard library, with no Cargo dependencies, filesystem access, networking, subprocesses, unsafe code, or dynamic loading.

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 until EOF, a read failure, or the 65537-byte read bound is reached. No timeout is provided.
  • Memory use is O(B + L), where B is the bounded input size and L is the retained-line count. Storage includes the input buffer, up to 100 borrowed line references, up to 100 integers, the output string, and allocator or I/O overhead.
  • All input validation finishes before successful stdout is emitted. Memory-allocation failures and output-write failures, including failure to write an error diagnostic, are outside the input-error guarantee. A failed stdout write may leave partial output.
  • Successful output reveals sequence-break positions and may be sensitive. Error diagnostics never include input values.

Run it

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

Example input

1
2
2
0
5

Expected output

{"positions":[3,4]}
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, ()> {
    let mut bytes = Vec::new();
    io::stdin()
        .lock()
        .take(65537)
        .read_to_end(&mut bytes)
        .map_err(|_| ())?;
    if bytes.len() > 65536 {
        return Err(());
    }

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

    // Enforce all retained-line limits before interpreting any number.
    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 values = Vec::with_capacity(lines.len());
    for line in lines {
        let digits = line.as_bytes();
        if digits.len() > 1 && digits[0] == b'0' {
            return Err(());
        }
        let mut value = 0u32;
        for &digit in digits {
            if !digit.is_ascii_digit() {
                return Err(());
            }
            // The preceding iteration guarantees value <= 1,000,000,
            // so this arithmetic cannot overflow u32.
            value = value * 10 + u32::from(digit - b'0');
            if value > 1_000_000 {
                return Err(());
            }
        }
        values.push(value);
    }

    // All input is valid before constructing the successful response.
    let mut output = String::from("{\"positions\":[");
    let mut first = true;
    for i in 1..values.len() {
        if values[i] <= values[i - 1] {
            if !first {
                output.push(',');
            }
            first = false;
            output.push_str(&(i + 1).to_string());
        }
    }
    output.push_str("]}\n");
    Ok(output)
}

fn main() {
    if std::env::args_os().skip(1).next().is_some() {
        invalid_input();
    }
    let output = match prepare_output() {
        Ok(output) => output,
        Err(()) => invalid_input(),
    };
    if io::stdout().lock().write_all(output.as_bytes()).is_err() {
        // Output-write failures are outside the input-error guarantee.
        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 18 supplementary cases are executed in pinned, offline Rust 1.99.0 containers. These checks do not establish exhaustive correctness or behavior on other versions.