4 ms·
> I feel like there should be a helper for simple parse operations. > parse!("#{} @ {},{}: {}x{}", id, x, y, w, h); > This would be a clean inverse of println
by dtolnay 8y ago
> I feel like there should be a helper for simple parse operations.
> parse!("#{} @ {},{}: {}x{}", id, x, y, w, h);
> This would be a clean inverse of println!.
The text_io crate does this exactly.
https://crates.io/crates/text_io https://crates.io/crates/text_io
#[macro_use]
extern crate text_io;
fn main() {
let id: u32;
let x: u32;
let y: u32;
let w: u32;
let h: u32;
scan!("#{} @ {},{}: {}x{}", id, x, y, w, h);
}
- nicoburns 8y agoOhhh, now that's super handy.
- nixpulvis 8y agoI wonder if we'll ever see `scan!` in the STD lib...
- josephg 8y agoI hope not. With a healthy crate ecosystem, std should be as small as possible (without being too small). It makes sense for std to contain: - Types that can't easily be put in a module (eg the `Fn` trait) - Types needed for cross-project interoperability (like `TcpStream` or `Future`). Having these in std helps prevent ecosystem fragmentation. - Stuff that gets used in most non-trivial projects (eg `Box`, `Vec`, `println!`) Everything else belongs in a crate, where it won't bloat up the size of rust's standard library for everyone in perpetuity. `scan!` looks great; but I expect it'll be used in less than 1% of projects. There's no shame in keeping it in a crate - thats what they're for!
- saghm 8y agoI see this come up a lot for various things in the Rust ecosystem. I think that moving more things into the standard library would actually be a net loss, mainly due to the versioning being tied to that of Rust itself. Once something is moved to the standard library, there can never be any breaking changes unless Rust 2.0 becomes a thing (which currently the core team says is not going to happen). It also makes the burden of doing bugfix releases much higher; for smaller bugs, users will have to wait until the next release of Rust itself, and for larger bugs, the entire language will have to do another release ahead of schedule. Given how easy Cargo makes it to add a dependency and how few projects can't actually use Cargo, I think it's better for the overwhelming majority of the ecosystem to keep things like this out of the standard library.
- Spiritus 8y agoThis is "stolen" from r/rust. But there's also a `scan!` macro in serde_scan[1] let line = "#1 @ 555,891: 18x12"; let parsed = scan!("#{} @ {},{}: {}x{}" <- line)?; [1] https://docs.rs/serde_scan/0.3.2/serde_scan/macro.scan.html https://docs.rs/serde_scan/0.3.2/serde_scan/macro.scan.html