Skip to content

Commit fdf0e3a

Browse files
author
Stjepan Glavina
committed
Add the line-count example
1 parent e44451a commit fdf0e3a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

examples/line-count.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Counts the number of lines in a file given as an argument.
2+
3+
#![feature(async_await)]
4+
5+
use std::env::args;
6+
7+
use async_std::fs::File;
8+
use async_std::io::{self, BufReader};
9+
use async_std::prelude::*;
10+
use async_std::task;
11+
12+
fn main() -> io::Result<()> {
13+
let path = args().nth(1).expect("missing path argument");
14+
15+
task::block_on(async {
16+
let file = BufReader::new(File::open(&path).await?);
17+
18+
let mut lines = file.lines();
19+
let mut count = 0u64;
20+
21+
while let Some(line) = lines.next().await {
22+
line?;
23+
count += 1;
24+
}
25+
26+
println!("The file contains {} lines.", count);
27+
Ok(())
28+
})
29+
}

0 commit comments

Comments
 (0)