Skip to content

undersquire/scanio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

scanio

RFC: rust-lang/rfcs#3183

This crate is the testing/WIP implementation of some simple macros for generic text input scanning (to accompany the print family of macros).

This crate currently implements four macros, scan!, try_scan!, read! and try_read!.

The implementation for these macros is experimental.

scan! usage:

#[macro_use]
extern crate scanio;

fn main() {
    let name: String;
    let age: u8;
    
    // reads a String into `name` and a u8 into `age`
    // if it fails, it will simply assign Default::default()
    scan!("{} {}", name, age);
    
    println!("{} of age {}", name, age);
}

try_scan! usage:

#[macro_use]
extern crate scanio;

fn main() {
    // returns a Result<(String, u8), ()>, which we unwrap
    let person = try_scan!("{} {}", String, u8).expect("Invalid input!");
    
    // ideally you should `match` on the result but this is an example so :shrug:
    
    println!("{} of age {}", person.0, person.1);
}

The read! and try_read! macros work exactly the same, however their first argument must be a mutable reference to an object that implements the std::io::Read trait, such as std::io::stdin() or a File.

About

Simple console input macros with the goal of being implemented in the standard library.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages