darkly
darkly copied to clipboard
Compare to mmun's spec
https://gist.github.com/mmun/bffc62a5eb6ad648b7f7b2825364962c (cc @mmun)
- [ ] - Add
scan!
for scan-not-a-whole-line case. - [ ] - Robust parsing:
scan!
should keep trying to match the input until it finds something that matches or we run out of bytes. This matchescin
andscanf
semantics. - [x] - Expression form of
scan!(...)
to avoid boilerplatey bindings in some cases, e.g.
// this snippet also uses default format string inference on the next line
for i in 0..scan!("{}") {
for j in 0..scan!("{}") {
// ...
}
}
- [ ] - Let
scan!()
infer a default format string:-
let x: i32 = scan!(); // "{}"
-
let x: (i32,) = scan!(); // "{}"
-
let x: (i32, i32) = scan!(); // "{}{}"
-
I just pushed a commit which adds the expression form. It always returns a tuple of results, which is not great for single values, but that should be fairly easy to add. E.g.,
let x: i32 = scan!("hello {}"); // todo
let x: (i32,) = scan!("hello {}"); // works
let x: (i32, i32) = scan!("hello {}"); // works
Awesome. I came to the same conclusion after several months of using scan!("hello {}").0
in my own library -- supporting singular values is just way more ergonomic!
@nrc Should it be "hello {} {}"
in your third line? Or am I misunderstanding.
it should
Another thing that comes up often is scanning individual characters. E.g. I'd expect that for the input string "123"
,
let (x: char, y: u8) = scan!("{}{}") // x == '1', y == 23
We could allow an empty format string in the statement form too: scanln!(x: i32)
is equivalent to scanln!("{}", x: i32)
I came to the same conclusion after several months of using
Single values are not tuple-wrapped now