Skip to content

lines example in docs doesn't work #85

Closed
@kespindler

Description

@kespindler

In https://docs.rs/async-std/0.99.3/async_std/io/trait.BufRead.html#examples-2, this example is provided:

use async_std::fs::File;
use async_std::io::BufReader;
use async_std::prelude::*;

let file = File::open("a.txt").await?;
let mut lines = BufReader::new(file).lines();
let mut count = 0;

for line in lines.next().await {
    line?;
    count += 1;
}

I used it in this full program:

#![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");

    let mut count = 0u64;
    task::block_on(async {
        //jlet file = File::open(&path).await?;
        //let mut lines = BufReader::new(file).lines();
        
        let file = File::open(&path).await?;
        let mut lines = BufReader::new(file).lines();
        let mut count = 0;

        for line in lines.next().await {
            line?;
            count += 1;
        }
        println!("The file contains {} lines.", count);
        Ok(())
    })
}

However, running that counts 1 line for any file with >= 1 line that I run it on. In contrast, this full program works correctly:

#![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");

    let mut count = 0u64;
    task::block_on(async {
        let file = File::open(&path).await?;
        let mut lines = BufReader::new(file).lines();

        while let Some(line) = lines.next().await {
            line?;
            count += 1;
        }

        println!("The file contains {} lines.", count);
        Ok(())
    })
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingdocumentationImprovements or additions to documentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions