mirror of
				https://github.com/async-rs/async-std.git
				synced 2025-10-31 00:36:38 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			26 lines
		
	
	
	
		
			633 B
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			26 lines
		
	
	
	
		
			633 B
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! Counts the number of lines in a file given as an argument.
 | |
| 
 | |
| 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 = File::open(&path).await?;
 | |
|         let mut lines = BufReader::new(file).lines();
 | |
|         let mut count = 0u64;
 | |
| 
 | |
|         while let Some(line) = lines.next().await {
 | |
|             line?;
 | |
|             count += 1;
 | |
|         }
 | |
| 
 | |
|         println!("The file contains {} lines.", count);
 | |
|         Ok(())
 | |
|     })
 | |
| }
 |