Reading large files in rust

59 points mmmuuukkk

I am creating a text editor with tauri and rust and needed some help reading large files. my current approach is this

#[tauri::command] 
async fn readFile(path: String) -> Result<String,errors::Error> { 
    addLog("Reading file");    
    let mut f = tokio::fs::File::open(path).await?;
    let fileSize = f.metadata().await?.len();
    let mut buffer = Vec::with_capacity(fileSize as usize);

    f.read_to_end(&mut buffer).await?;
    Ok(String::from_utf8_lossy(&buffer).to_string())
}

this works great but when reading large files it becomes unbearably slow. I need some suggestions on how I can improve the performance of this function.