Open nrc opened 7 years ago
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
https://gist.github.com/mmun/bffc62a5eb6ad648b7f7b2825364962c (cc @mmun)
scan!
for scan-not-a-whole-line case.scan!
should keep trying to match the input until it finds something that matches or we run out of bytes. This matchescin
andscanf
semantics.scan!(...)
to avoid boilerplatey bindings in some cases, e.g.scan!()
infer a default format string:let x: i32 = scan!(); // "{}"
let x: (i32,) = scan!(); // "{}"
let x: (i32, i32) = scan!(); // "{}{}"