1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// Some examples of reading from standard input using the C-compatible feature.
///
/// We use a `v` to indicate the cursor position.
#[cfg(feature = "c-compatible")]
fn main() {
use iof::read;
// Read integers from input, that are not separated by spaces.
//
// v
// 1+2
let (a, op, b): (i32, char, u32) = read!();
assert_eq!(a, 1);
assert_eq!(op, '+');
assert_eq!(b, 2);
// Or float numbers.
//
// v
// 1.2-3.4
let (a, op, b): (f64, char, f64) = read!();
assert_eq!(a, 1.2);
assert_eq!(op, '-');
assert_eq!(b, 3.4);
}
/// Without the C-compatible feature, this function will not be compiled.
#[cfg(not(feature = "c-compatible"))]
fn main() {
use iof::try_read;
// Read integers from input, that are not separated by spaces.
// As `1+2` could be interpreted as a single integer, this will fail.
//
// v
// 1+2
let a: Result<i32, _> = try_read();
assert!(a.is_err());
// Or float numbers.
// Similarly, `1.2-3.4` could be interpreted as a single float number.
//
// v
// 1.2-3.4
let a: Result<f64, _> = try_read();
assert!(a.is_err());
}