CIO - Console Input/Output for Rust
CIO provides two powerful procedural macros (println! and input!) that bring Python-like convenience to Rust's type-safe environment. These macros streamline console interaction with advanced formatting and validation capabilities.
Installation
Add this to your Cargo.toml:
[]
= "0.3.0"
Features Overview
println! macro
- Python f-string style direct variable insertion (
{var}instead of{}with separate arguments) - ANSI color and style formatting with
@(...)syntax - Special formatters for data structures and collections (
:a,:c,:j,:m,:d) - Separator control with
$(...)syntax to unifyprint!andprintln! - Rich expression evaluation in format strings
- Pretty-print for all
std::collectionstypes with intuitive formatting
input! macro
- Type-safe input with automatic parsing and validation
- Error handling with colored error messages and re-prompting
- Support for multiple types (strings, numbers, booleans, chars)
- Validation of input length (especially for
charinputs)
Examples
1. Basic Input and Output
use ;
// Type-safe input collection with validation
let name: String = input!;
let age: i32 = input!;
let height: f64 = input!;
let married: bool = input!;
let favorite_letter: char = input!;
// Direct variable names in placeholders (Python f-string style)
println!;
2. Colored and Styled Output
// Apply colors and styles with @(...) syntax
println!;
println!;
println!;
// Mix variables with styling
println!;
3. Rich Expression Evaluation
// Direct expressions in format placeholders
println!;
println!;
println!;
// Conditional expressions
println!;
// Method calls
println!;
4. Separator Control (Unifying print! and println!)
// No separator - works like println!
println!;
// With separator - works like print! with explicit separator
for i in 1..10
// Dynamic separators with variables
let separator = " | ";
println!;
5. Container Formatting
use ;
// Arrays and vectors
let numbers = vec!;
println!; // [1, 2, 3, 4, 5]
// Matrices
let matrix = vec!;
println!; // Indented array
println!; // With matrix borders
println!; // With determinant bars
// HashMap
let mut capitals = new;
capitals.insert;
capitals.insert;
println!; // JSON-like pretty format
println!; // Single-line compact format
// Other collections
let queue = from;
println!; // Pretty prints any collection
6. Nested Structures
// Deeply nested data
let nested = vec!;
println!;
// 3D arrays
let cube = vec!;
println!; // Properly indented 3D structure
7. Input Validation
// String inputs (can't be empty)
let username: String = input!;
// Numeric inputs (validates correct type)
let score: u32 = input!; // Rejects non-numbers or floats
// Boolean inputs (accepts various formats)
let proceed: bool = input!; // Accepts "true"/"false", "yes"/"no", "y"/"n", "1"/"0"
// Character inputs (ensures single character)
let grade: char = input!; // Rejects multiple characters
8. Colored Input Prompts
// Styled input prompts
let name = input!;
let age = input!;
// Errors are automatically displayed in red with blinking
// Error: invalid digit found in string
Available Colors and Styles
Colors
- Basic:
black,red,green,yellow,blue,magenta,cyan,white - Bright:
bright_black,bright_red,bright_green,bright_yellow,bright_blue,bright_magenta,bright_cyan,bright_white,gray(alias forbright_black)
Styles
bold,italic,underline,dimmed,blink,reversed,hidden,strikethrough
Format Specifiers
:a- Array format with proper indentation for nested structures:c- Compact single-line format for any data structure:j- JSON-like pretty format for complex structures:m- Matrix format with proper borders for 2D arrays:d- Determinant format with vertical bars
Key Benefits
- Python-like Simplicity: Familiar syntax for Python users with Rust's type safety
- Reduced Boilerplate: Eliminate repetitive input/output code patterns
- Visual Enhancement: Easily add colors and styles for better UX
- Data Visualization: Beautiful display of complex data structures
- Unified Interface: Consistent syntax for all output formatting needs
- Type Safety: Maintain Rust's safety guarantees with convenient syntax
How It Compares to Python
CIO combines the best of both worlds:
- Like Python's f-strings: Direct variable names in format strings (
{var}) - Like Python's input(): Single-line input collection
- Beyond Python: Type validation, error handling, and rich formatting
- Rust Advantage: Maintains full type safety while offering convenience
License
This project is licensed under the MIT License.
CIO: Making console interaction in Rust as enjoyable as Python, but with the safety of Rust.