-
Notifications
You must be signed in to change notification settings - Fork 1
/
9.rs
46 lines (41 loc) · 1.37 KB
/
9.rs
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
use std::io;
/// Copy input to output, replacing each string of one or more blanks by a single blank.
fn main()
{
// a mutable variable to hold input data
let mut input = String::new();
let mut blanks: u32 = 0;
// using rust infinite loop
loop {
match io::stdin().read_line(&mut input) {
// io::Result::Ok receives the number of bytes read
Ok (n_bytes) => {
// case EOF is reached, "read_lines" will return 0 and we break out of the loop
if n_bytes == 0 {
break;
}
// iterate list of chars read from input
let _c : char;
for _c in input.chars() {
match _c {
' ' => {
if blanks == 0 {
print!(" ");
}
blanks += 1;
},
_ => {
print!("{}", _c);
blanks = 0;
},
}
}
// we need to clear input buffer because "read_line" appends all read bytes to it
input.clear();
},
Err(_) => {
panic!("Unexpected error.");
}
}
}
}