

std::io::Seek - Rust
source link: https://doc.rust-lang.org/stable/std/io/trait.Seek.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Trait std::io::Seek1.0.0[−][src]
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
File
s 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(()) }Run
Required methods
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
[src][−]
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.
Provided methods
fn stream_len(&mut self) -> Result<u64>
[src][−]
seek_stream_len
#59359)Returns the length of this stream (in bytes).
This method is implemented using up to three seek operations. If this method returns successfully, the seek position is unchanged (i.e. the position before calling this method is the same as afterwards). However, if this method returns an error, the seek position is unspecified.
If you need to obtain the length of many streams and you don't care
about the seek position afterwards, you can reduce the number of seek
operations by simply calling seek(SeekFrom::End(0))
and using its
return value (it is also the stream length).
Note that length of a stream can change over time (for example, when data is appended to a file). So calling this method multiple times does not necessarily return the same length each time.
Example
#![feature(seek_stream_len)] use std::{ io::{self, Seek}, fs::File, }; fn main() -> io::Result<()> { let mut f = File::open("foo.txt")?; let len = f.stream_len()?; println!("The file is currently {} bytes long", len); Ok(()) }Run
fn stream_position(&mut self) -> Result<u64>
1.51.0[src][−]
Returns the current seek position from the start of the stream.
This is equivalent to self.seek(SeekFrom::Current(0))
.
Example
use std::{ io::{self, BufRead, BufReader, Seek}, fs::File, }; fn main() -> io::Result<()> { let mut f = BufReader::new(File::open("foo.txt")?); let before = f.stream_position()?; f.read_line(&mut String::new())?; let after = f.stream_position()?; println!("The first line was {} bytes long", after - before); Ok(()) }Run
Implementors
Recommend
-
7
1.0.0[−][src]Module std::result[−]Er...
-
12
Variants Ready(T)[−]Represents that a value is immediately ready. Pending[−]Represents that a value is not read...
-
14
MILAN🦀 std::process::exit is evil - a Rust bug fixing story912 views•Jan 17...
-
11
Struct std::iter::Peekable1.0.0[...
-
8
Struct std::sync::Once1.0.0[−]
-
9
Function std::panic::panic_any1.51.0...
-
9
impl<T, const N: usize> IntoIter<T, N>
-
12
Struct std::sync::OnceState1.51.0
-
10
Macro std::ptr::addr_of1.51.0[−]...
-
7
Copy link Task lists! Give feedback ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK