Pls Turn this code into async with futures

fn cat(path: &Path) -> io::Result {
let mut f = File::open(path)?;
let mut s = String::new();
match f.read_to_string(&mut s) {
Ok(_) => Ok(s),
Err(e) => Err(e),
}

The above code is blocking right..pls turn that into async with Future trait implementation

Please follow our syntax highlighting guidelines

use std::io;
use std::path::Path;
use tokio::fs::File;
use tokio::io::AsyncReadExt;

async fn cat(path: &Path) -> io::Result<String> {
    let mut f = File::open(path).await?;
    let mut s = String::new();
    match f.read_to_string(&mut s).await {
        Ok(_) => Ok(s),
        Err(e) => Err(e),
    }
}
3 Likes

Thanks now am understand..

Last and final can u pls give the poll method implementation of the File.open() method of the above example .

Just for the idea..

The only poll method is enough..
Thanks

the source of that method is available on github. A search for tokio should turn up the repo pretty quickly.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.