Warning
This post was published 55 days ago. The information described in this article may have changed.
Hello Rust community!
I'm excited to share my crate CIO which 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.
Add this to your Cargo.toml
:
[dependencies]
cio = "0.4.0"
println!
macro{var}
instead of {}
with separate arguments)@(...)
syntax:a
, :c
, :j
, :m
, :d
)$(...)
syntax to unify print!
and println!
std::collections
types with intuitive formattinginput!
macrochar
inputs)use cio::{println, input};
// Type-safe input collection with validation
let name: String = input!("Your name: ");
let age: i32 = input!("Your age: ");
let height: f64 = input!("Your height (in meters): ");
let married: bool = input!("Are you married? (true/false): ");
let favorite_letter: char = input!("What is your favorite letter? ");
// Direct variable names in placeholders (Python f-string style)
println!("Hello, {name}, you are {age} years old!");
// Apply colors and styles with @(...) syntax
println!("@(green, bold)Success:@() Operation completed!");
println!("@(red, italic)Error:@() Something went wrong.");
println!("@(blue)Information:@() System is @(yellow)running@() normally.");
// Mix variables with styling
println!("@(bright_cyan, bold)User:@() {name} (@(bright_green){age}@() years old)");
// Direct expressions in format placeholders
println!("Age in months: {age * 12}");
println!("Height in cm: {height * 100.0:.0}");
println!("Name in uppercase: {name.to_uppercase()}");
// Conditional expressions
println!("Status: {if age >= 18 { "Adult" } else { "Minor" }}");
// Method calls
println!("First letter: {name.chars().next().unwrap_or('?')}");
// No separator - works like println!
println!("Regular line with newline");
// With separator - works like print! with explicit separator
for i in 1..10 {
println!("{i}$( - )"); // Prints "1 - 2 - 3 - ..." on one line
}
// Dynamic separators with variables
let separator = " | ";
println!("Item: {item}$(separator)");
use std::collections::{HashMap, BTreeMap, VecDeque};
// Arrays and vectors
let numbers = vec![1, 2, 3, 4, 5];
println!("Numbers (array format): {numbers:a}"); // [1, 2, 3, 4, 5]
// Matrices
let matrix = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
println!("Matrix (standard format):\n{matrix:a}"); // Indented array
println!("Matrix (matrix format):\n{matrix:m}"); // With matrix borders
println!("Matrix (determinant format):\n{matrix:d}"); // With determinant bars
// HashMap
let mut capitals = HashMap::new();
capitals.insert("France", "Paris");
capitals.insert("Japan", "Tokyo");
println!("Capitals (pretty):\n{capitals:j}"); // JSON-like pretty format
println!("Capitals (compact): {capitals:c}"); // Single-line compact format
// Other collections
let queue = VecDeque::from([1, 2, 3, 4]);
println!("Queue: {queue:a}"); // Pretty prints any collection
// Deeply nested data
let nested = vec![
HashMap::from([
("name", "Alice"),
("scores", &[95, 87, 92])
]),
HashMap::from([
("name", "Bob"),
("scores", &[78, 85, 90])
])
];
println!("Nested data (pretty):\n{nested:j}");
// 3D arrays
let cube = vec![
vec![vec![1, 2], vec![3, 4]],
vec![vec![5, 6], vec![7, 8]]
];
println!("3D array:\n{cube:a}"); // Properly indented 3D structure
// String inputs (can't be empty)
let username: String = input!("Username: ");
// Numeric inputs (validates correct type)
let score: u32 = input!("Score (0-100): "); // Rejects non-numbers or floats
// Boolean inputs (accepts various formats)
let proceed: bool = input!("Continue? (y/n): "); // Accepts "true"/"false", "yes"/"no", "y"/"n", "1"/"0"
// Character inputs (ensures single character)
let grade: char = input!("Grade (A-F): "); // Rejects multiple characters
// Styled input prompts
let name = input!("@(green, bold)Your name:@() ");
let age = input!("@(bright_cyan)Your age:@() ");
// Errors are automatically displayed in red with blinking
// Error: invalid digit found in string
black
, red
, green
, yellow
, blue
, magenta
, cyan
, white
bright_black
, bright_red
, bright_green
, bright_yellow
, bright_blue
, bright_magenta
, bright_cyan
, bright_white
, gray
(alias for bright_black
)bold
, italic
, underline
, dimmed
, blink
, reversed
, hidden
, strikethrough
: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 barsCIO combines the best of both worlds:
{var}
)I welcome all feedback, suggestions for improvement, and contributions! Feel free to open issues on GitHub or submit pull requests.
Thank you for checking out CIO. I hope it makes your console interaction in Rust more enjoyable!
1 post - 1 participant
🏷️ rust_feed