Crates.io | scanio |
lib.rs | scanio |
version | 0.1.0 |
source | src |
created_at | 2021-11-15 19:20:24.910688 |
updated_at | 2021-11-15 19:20:24.910688 |
description | Simple console input macros with the goal of being implemented in the standard library. |
homepage | https://github.com/undersquire/scanio |
repository | https://github.com/undersquire/scanio |
max_upload_size | |
id | 482310 |
size | 10,390 |
RFC: https://github.com/rust-lang/rfcs/pull/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!
.
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
.