std-type

Cursor: seekable in-memory I/O

A Cursor adds a byte position to in-memory data. It starts at position zero even for a nonempty vector, so writing there overwrites existing bytes instead of appending. It is useful for deterministic I/O tests. Expected stdout (not executed): 1 Zbc Common mistake: Assuming a cursor over a vector starts at the end.

Formal Syntax & Structure

rust syntaxrust
Cursor::new(buffer); position(); set_position(pos); into_inner()

Working Example

Example Snippetrust
fn main() {    use std::io::{Cursor, Write};    let mut c = Cursor::new(b"abc".to_vec());    c.write_all(b"Z").unwrap();    println!("{} {}", c.position(), String::from_utf8(c.into_inner()).unwrap());}
Rust Reference