mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-16 10:49:55 +00:00
Add the line-count example
This commit is contained in:
parent
e44451a042
commit
fdf0e3ad0f
1 changed files with 29 additions and 0 deletions
29
examples/line-count.rs
Normal file
29
examples/line-count.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
//! Counts the number of lines in a file given as an argument.
|
||||||
|
|
||||||
|
#![feature(async_await)]
|
||||||
|
|
||||||
|
use std::env::args;
|
||||||
|
|
||||||
|
use async_std::fs::File;
|
||||||
|
use async_std::io::{self, BufReader};
|
||||||
|
use async_std::prelude::*;
|
||||||
|
use async_std::task;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let path = args().nth(1).expect("missing path argument");
|
||||||
|
|
||||||
|
task::block_on(async {
|
||||||
|
let file = BufReader::new(File::open(&path).await?);
|
||||||
|
|
||||||
|
let mut lines = file.lines();
|
||||||
|
let mut count = 0u64;
|
||||||
|
|
||||||
|
while let Some(line) = lines.next().await {
|
||||||
|
line?;
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("The file contains {} lines.", count);
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue