Skip to main content

Seek

Trait Seek 

Source
pub trait Seek {
    // Required method
    fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
}
Expand description

The Seek trait provides a cursor which can be moved within a stream of bytes.

The stream typically has a fixed size, allowing seeking relative to either end or the current offset.

§Examples

Files implement Seek:

use std::io;
use std::io::prelude::*;
use std::fs::File;
use std::io::SeekFrom;

fn main() -> io::Result<()> {
    let mut f = File::open("foo.txt")?;

    // move the cursor 42 bytes from the start of the file
    f.seek(SeekFrom::Start(42))?;
    Ok(())
}

Required Methods§

Source

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to an offset, in bytes, in a stream.

A seek beyond the end of a stream is allowed, but behavior is defined by the implementation.

If the seek operation completed successfully, this method returns the new position from the start of the stream. That position can be used later with SeekFrom::Start.

§Errors

Seeking to a negative offset is considered an error.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<S: Seek + ?Sized> Seek for &mut S

Source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Implementors§

Source§

impl Seek for &File

Source§

impl Seek for File

Source§

impl<R: Seek> Seek for BufReader<R>

Source§

impl<S: Seek + ?Sized> Seek for Box<S>

Source§

impl<T> Seek for Cursor<T>
where T: AsRef<[u8]>,

Source§

impl<W: Write + Seek> Seek for BufWriter<W>