expression

Seek within an ordinary async file

Seek changes the position used by subsequent reads on a seekable file. Not every asynchronous source supports seeking. Use a fresh fixture, flush pending writes, and close before cleanup. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): cd Common mistake: Assuming network streams support file-style random access.

Formal Syntax & Structure

rust syntaxrust
AsyncSeekExt::seek(SeekFrom::Start(offset)).await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};    let path = "bm-v61-seek.txt";    let mut f = tokio::fs::OpenOptions::new().read(true).write(true).create_new(true).open(path).await.unwrap();    f.write_all(b"abcdef").await.unwrap();    f.flush().await.unwrap();    f.seek(std::io::SeekFrom::Start(2)).await.unwrap();    let mut bytes = [0; 2];    f.read_exact(&mut bytes).await.unwrap();    println!("{}", String::from_utf8_lossy(&bytes));    drop(f);    tokio::fs::remove_file(path).await.unwrap();}
Rust Reference