The io module contains basic input and output routines.

A quick summary:

Reader and Writer traits

These traits define the minimal set of methods that anything that can do input and output should implement.

ReaderUtil and WriterUtil traits

Richer methods that allow you to do more. Reader only lets you read a certain number of bytes into a buffer, while ReaderUtil allows you to read a whole line, for example.

Generally, these richer methods are probably the ones you want to actually use in day-to-day Rust.

Furthermore, because there is an implementation of ReaderUtil for <T: Reader>, when your input or output code implements Reader, you get all of these methods for free.

stdin, stdout, and stderr

These functions return references to the classic three file descriptors. They implement Reader and Writer, where appropriate.

Type fd_t

type fd_t = c_int

Enum FileFlag

Variants

Enum SeekStyle

The SeekStyle enum describes the relationship between the position we'd like to seek to from our current position. It's used as an argument to the seek method defined on the Reader trait.

There are three seek styles:

  1. SeekSet means that the new position should become our position.
  2. SeekCur means that we should seek from the current position.
  3. SeekEnd means that we should seek from the end.

Examples

None right now.

Variants

Enum WriterType

Variants

Struct BytesReader

pub struct BytesReader {
    bytes: &'static [u8],
    pos: @mut uint,
}

Struct BytesWriter

pub struct BytesWriter {
    bytes: @mut ~[u8],
    pos: @mut uint,
}

Struct FILERes

pub struct FILERes {
    f: *libc::FILE,
}

Struct FdRes

pub struct FdRes {
    fd: fd_t,
}

Trait Reader

The core Reader trait. All readers must implement this trait.

Examples

None right now.

Method read

fn read(&self, bytes: &mut [u8], len: uint) -> uint

Reads bytes and puts them into bytes, advancing the cursor. Returns the number of bytes read.

The number of bytes to be read is len or the end of the file, whichever comes first.

The buffer must be at least len bytes long.

read is conceptually similar to C's fread function.

Examples

None right now.

Method read_byte

fn read_byte(&self) -> int

Reads a single byte, advancing the cursor.

In the case of an EOF or an error, returns a negative value.

read_byte is conceptually similar to C's getc function.

Examples

None right now.

Method eof

fn eof(&self) -> bool

Returns a boolean value: are we currently at EOF?

Note that stream position may be already at the end-of-file point, but eof returns false until an attempt to read at that position.

eof is conceptually similar to C's feof function.

Examples

None right now.

Method seek

fn seek(&self, position: int, style: SeekStyle)

Seek to a given position in the stream.

Takes an optional SeekStyle, which affects how we seek from the position. See SeekStyle docs for more details.

seek is conceptually similar to C's fseek function.

Examples

None right now.

Method tell

fn tell(&self) -> uint

Returns the current position within the stream.

tell is conceptually similar to C's ftell function.

Examples

None right now.

Trait ReaderUtil

The ReaderUtil trait is a home for many of the utility functions a particular Reader should implement.

The default Reader trait is focused entirely on bytes. ReaderUtil is based on higher-level concepts like 'chars' and 'lines.'

Examples:

None right now.

Method read_bytes

fn read_bytes(&self, len: uint) -> ~[u8]

Reads len number of bytes, and gives you a new vector back.

Examples

None right now.

Method read_until

fn read_until(&self, c: u8, include: bool) -> ~str

Reads up until a specific byte is seen or EOF.

The include parameter specifies if the character should be included in the returned string.

Examples

None right now.

Method read_line

fn read_line(&self) -> ~str

Reads up until the first '\n' or EOF.

The '\n' is not included in the result.

Examples

None right now.

Method read_chars

fn read_chars(&self, n: uint) -> ~[char]

Reads n chars.

Assumes that those chars are UTF-8 encoded.

The '\n' is not included in the result.

Examples

None right now.

Method read_char

fn read_char(&self) -> char

Reads a single UTF-8 encoded char.

Examples

None right now.

Method read_c_str

fn read_c_str(&self) -> ~str

Reads up until the first null byte or EOF.

The null byte is not returned.

Examples

None right now.

Method read_whole_stream

fn read_whole_stream(&self) -> ~[u8]

Reads all remaining data in the stream.

Examples

None right now.

Method each_byte

fn each_byte(&self, it: &fn(int) -> bool) -> bool

Iterate over every byte until EOF or the iterator breaks.

Examples

None right now.

Method each_char

fn each_char(&self, it: &fn(char) -> bool) -> bool

Iterate over every char until EOF or the iterator breaks.

Examples

None right now.

Method each_line

fn each_line(&self, it: &fn(&str) -> bool) -> bool

Iterate over every line until EOF or the iterator breaks.

Examples

None right now.

Method read_lines

fn read_lines(&self) -> ~[~str]

Reads all of the lines in the stream.

Returns a vector of those lines.

Examples

None right now.

Method read_le_uint_n

fn read_le_uint_n(&self, nbytes: uint) -> u64

Reads n little-endian unsigned integer bytes.

n must be between 1 and 8, inclusive.

Examples

None right now.

Method read_le_int_n

fn read_le_int_n(&self, nbytes: uint) -> i64

Reads n little-endian signed integer bytes.

n must be between 1 and 8, inclusive.

Examples

None right now.

Method read_be_uint_n

fn read_be_uint_n(&self, nbytes: uint) -> u64

Reads n big-endian unsigned integer bytes.

n must be between 1 and 8, inclusive.

Examples

None right now.

Method read_be_int_n

fn read_be_int_n(&self, nbytes: uint) -> i64

Reads n big-endian signed integer bytes.

n must be between 1 and 8, inclusive.

Examples

None right now.

Method read_le_uint

fn read_le_uint(&self) -> uint

Reads a little-endian unsigned integer.

The number of bytes returned is system-dependant.

Examples

None right now.

Method read_le_int

fn read_le_int(&self) -> int

Reads a little-endian integer.

The number of bytes returned is system-dependant.

Examples

None right now.

Method read_be_uint

fn read_be_uint(&self) -> uint

Reads a big-endian unsigned integer.

The number of bytes returned is system-dependant.

Examples

None right now.

Method read_be_int

fn read_be_int(&self) -> int

Reads a big-endian integer.

The number of bytes returned is system-dependant.

Examples

None right now.

Method read_be_u64

fn read_be_u64(&self) -> u64

Reads a big-endian u64.

u64s are 8 bytes long.

Examples

None right now.

Method read_be_u32

fn read_be_u32(&self) -> u32

Reads a big-endian u32.

u32s are 4 bytes long.

Examples

None right now.

Method read_be_u16

fn read_be_u16(&self) -> u16

Reads a big-endian u16.

u16s are 2 bytes long.

Examples

None right now.

Method read_be_i64

fn read_be_i64(&self) -> i64

Reads a big-endian i64.

i64s are 8 bytes long.

Examples

None right now.

Method read_be_i32

fn read_be_i32(&self) -> i32

Reads a big-endian i32.

i32s are 4 bytes long.

Examples

None right now.

Method read_be_i16

fn read_be_i16(&self) -> i16

Reads a big-endian i16.

i16s are 2 bytes long.

Examples

None right now.

Method read_be_f64

fn read_be_f64(&self) -> f64

Reads a big-endian f64.

f64s are 8 byte, IEEE754 double-precision floating point numbers.

Examples

None right now.

Method read_be_f32

fn read_be_f32(&self) -> f32

Reads a big-endian f32.

f32s are 4 byte, IEEE754 single-precision floating point numbers.

Examples

None right now.

Method read_le_u64

fn read_le_u64(&self) -> u64

Reads a little-endian u64.

u64s are 8 bytes long.

Examples

None right now.

Method read_le_u32

fn read_le_u32(&self) -> u32

Reads a little-endian u32.

u32s are 4 bytes long.

Examples

None right now.

Method read_le_u16

fn read_le_u16(&self) -> u16

Reads a little-endian u16.

u16s are 2 bytes long.

Examples

None right now.

Method read_le_i64

fn read_le_i64(&self) -> i64

Reads a little-endian i64.

i64s are 8 bytes long.

Examples

None right now.

Method read_le_i32

fn read_le_i32(&self) -> i32

Reads a little-endian i32.

i32s are 4 bytes long.

Examples

None right now.

Method read_le_i16

fn read_le_i16(&self) -> i16

Reads a little-endian i16.

i16s are 2 bytes long.

Examples

None right now.

Method read_le_f64

fn read_le_f64(&self) -> f64

Reads a little-endian f64.

f64s are 8 byte, IEEE754 double-precision floating point numbers.

Examples

None right now.

Method read_le_f32

fn read_le_f32(&self) -> f32

Reads a little-endian f32.

f32s are 4 byte, IEEE754 single-precision floating point numbers.

Examples

None right now.

Method read_u8

fn read_u8(&self) -> u8

Read a u8.

u8s are 1 byte.

Examples

None right now.

Method read_i8

fn read_i8(&self) -> i8

Read an i8.

i8s are 1 byte.

Examples

None right now.

Trait Writer

The raw underlying writer trait. All writers must implement this.

Method write

fn write(&self, v: &[u8])

Write all of the given bytes.

Method seek

fn seek(&self, int, SeekStyle)

Move the current position within the stream. The second parameter determines the position that the first parameter is relative to.

Method tell

fn tell(&self) -> uint

Return the current position within the stream.

Method flush

fn flush(&self) -> int

Flush the output buffer for this stream (if there is one).

Method get_type

fn get_type(&self) -> WriterType

Determine if this Writer is writing to a file or not.

Trait WriterUtil

Generic utility functions defined on writers.

Method write_char

fn write_char(&self, ch: char)

Write a single utf-8 encoded char.

Method write_str

fn write_str(&self, s: &str)

Write every char in the given str, encoded as utf-8.

Method write_line

fn write_line(&self, s: &str)

Write the given str, as utf-8, followed by '\n'.

Method write_int

fn write_int(&self, n: int)

Write the result of passing n through int::to_str_bytes.

Method write_uint

fn write_uint(&self, n: uint)

Write the result of passing n through uint::to_str_bytes.

Method write_le_uint

fn write_le_uint(&self, n: uint)

Write a little-endian uint (number of bytes depends on system).

Method write_le_int

fn write_le_int(&self, n: int)

Write a little-endian int (number of bytes depends on system).

Method write_be_uint

fn write_be_uint(&self, n: uint)

Write a big-endian uint (number of bytes depends on system).

Method write_be_int

fn write_be_int(&self, n: int)

Write a big-endian int (number of bytes depends on system).

Method write_be_u64

fn write_be_u64(&self, n: u64)

Write a big-endian u64 (8 bytes).

Method write_be_u32

fn write_be_u32(&self, n: u32)

Write a big-endian u32 (4 bytes).

Method write_be_u16

fn write_be_u16(&self, n: u16)

Write a big-endian u16 (2 bytes).

Method write_be_i64

fn write_be_i64(&self, n: i64)

Write a big-endian i64 (8 bytes).

Method write_be_i32

fn write_be_i32(&self, n: i32)

Write a big-endian i32 (4 bytes).

Method write_be_i16

fn write_be_i16(&self, n: i16)

Write a big-endian i16 (2 bytes).

Method write_be_f64

fn write_be_f64(&self, f: f64)

Write a big-endian IEEE754 double-precision floating-point (8 bytes).

Method write_be_f32

fn write_be_f32(&self, f: f32)

Write a big-endian IEEE754 single-precision floating-point (4 bytes).

Method write_le_u64

fn write_le_u64(&self, n: u64)

Write a little-endian u64 (8 bytes).

Method write_le_u32

fn write_le_u32(&self, n: u32)

Write a little-endian u32 (4 bytes).

Method write_le_u16

fn write_le_u16(&self, n: u16)

Write a little-endian u16 (2 bytes).

Method write_le_i64

fn write_le_i64(&self, n: i64)

Write a little-endian i64 (8 bytes).

Method write_le_i32

fn write_le_i32(&self, n: i32)

Write a little-endian i32 (4 bytes).

Method write_le_i16

fn write_le_i16(&self, n: i16)

Write a little-endian i16 (2 bytes).

Method write_le_f64

fn write_le_f64(&self, f: f64)

Write a little-endian IEEE754 double-precision floating-point (8 bytes).

Method write_le_f32

fn write_le_f32(&self, f: f32)

Write a little-endian IEEE754 single-precision floating-point (4 bytes).

Method write_u8

fn write_u8(&self, n: u8)

Write a u8 (1 byte).

Method write_i8

fn write_i8(&self, n: i8)

Write a i8 (1 byte).

Implementation of Reader for @Reader

Method read

fn read(&self, bytes: &mut [u8], len: uint) -> uint

Method read_byte

fn read_byte(&self) -> int

Method eof

fn eof(&self) -> bool

Method seek

fn seek(&self, position: int, style: SeekStyle)

Method tell

fn tell(&self) -> uint

Implementation of ReaderUtil for T where <T: Reader>

Method read_bytes

fn read_bytes(&self, len: uint) -> ~[u8]

Method read_until

fn read_until(&self, c: u8, include: bool) -> ~str

Method read_line

fn read_line(&self) -> ~str

Method read_chars

fn read_chars(&self, n: uint) -> ~[char]

Method read_char

fn read_char(&self) -> char

Method read_c_str

fn read_c_str(&self) -> ~str

Method read_whole_stream

fn read_whole_stream(&self) -> ~[u8]

Method each_byte

fn each_byte(&self, it: &fn(int) -> bool) -> bool

Method each_char

fn each_char(&self, it: &fn(char) -> bool) -> bool

Method each_line

fn each_line(&self, it: &fn(s: &str) -> bool) -> bool

Method read_lines

fn read_lines(&self) -> ~[~str]

Method read_le_uint_n

fn read_le_uint_n(&self, nbytes: uint) -> u64

Method read_le_int_n

fn read_le_int_n(&self, nbytes: uint) -> i64

Method read_be_uint_n

fn read_be_uint_n(&self, nbytes: uint) -> u64

Method read_be_int_n

fn read_be_int_n(&self, nbytes: uint) -> i64

Method read_le_uint

fn read_le_uint(&self) -> uint

Method read_le_int

fn read_le_int(&self) -> int

Method read_be_uint

fn read_be_uint(&self) -> uint

Method read_be_int

fn read_be_int(&self) -> int

Method read_be_u64

fn read_be_u64(&self) -> u64

Method read_be_u32

fn read_be_u32(&self) -> u32

Method read_be_u16

fn read_be_u16(&self) -> u16

Method read_be_i64

fn read_be_i64(&self) -> i64

Method read_be_i32

fn read_be_i32(&self) -> i32

Method read_be_i16

fn read_be_i16(&self) -> i16

Method read_be_f64

fn read_be_f64(&self) -> f64

Method read_be_f32

fn read_be_f32(&self) -> f32

Method read_le_u64

fn read_le_u64(&self) -> u64

Method read_le_u32

fn read_le_u32(&self) -> u32

Method read_le_u16

fn read_le_u16(&self) -> u16

Method read_le_i64

fn read_le_i64(&self) -> i64

Method read_le_i32

fn read_le_i32(&self) -> i32

Method read_le_i16

fn read_le_i16(&self) -> i16

Method read_le_f64

fn read_le_f64(&self) -> f64

Method read_le_f32

fn read_le_f32(&self) -> f32

Method read_u8

fn read_u8(&self) -> u8

Method read_i8

fn read_i8(&self) -> i8

Implementation of Reader for *libc::FILE

Method read

fn read(&self, bytes: &mut [u8], len: uint) -> uint

Method read_byte

fn read_byte(&self) -> int

Method eof

fn eof(&self) -> bool

Method seek

fn seek(&self, offset: int, whence: SeekStyle)

Method tell

fn tell(&self) -> uint

Implementation of Reader for Wrapper<R, C> where <R: Reader, C>

Method read

fn read(&self, bytes: &mut [u8], len: uint) -> uint

Method read_byte

fn read_byte(&self) -> int

Method eof

fn eof(&self) -> bool

Method seek

fn seek(&self, off: int, whence: SeekStyle)

Method tell

fn tell(&self) -> uint

Implementation for FILERes

Method new

fn new(f: *libc::FILE) -> FILERes

Implementation of Drop for FILERes

Method drop

fn drop(&self)

Implementation of Reader for BytesReader

Method read

fn read(&self, bytes: &mut [u8], len: uint) -> uint

Method read_byte

fn read_byte(&self) -> int

Method eof

fn eof(&self) -> bool

Method seek

fn seek(&self, offset: int, whence: SeekStyle)

Method tell

fn tell(&self) -> uint

Implementation of ::std::cmp::Eq for WriterType

Automatically derived.

Method eq

fn eq(&self, __arg_0: &WriterType) -> ::bool

Method ne

fn ne(&self, __arg_0: &WriterType) -> ::bool

Implementation of Writer for @Writer

Method write

fn write(&self, v: &[u8])

Method seek

fn seek(&self, a: int, b: SeekStyle)

Method tell

fn tell(&self) -> uint

Method flush

fn flush(&self) -> int

Method get_type

fn get_type(&self) -> WriterType

Implementation of Writer for Wrapper<W, C> where <W: Writer, C>

Method write

fn write(&self, bs: &[u8])

Method seek

fn seek(&self, off: int, style: SeekStyle)

Method tell

fn tell(&self) -> uint

Method flush

fn flush(&self) -> int

Method get_type

fn get_type(&self) -> WriterType

Implementation of Writer for *libc::FILE

Method write

fn write(&self, v: &[u8])

Method seek

fn seek(&self, offset: int, whence: SeekStyle)

Method tell

fn tell(&self) -> uint

Method flush

fn flush(&self) -> int

Method get_type

fn get_type(&self) -> WriterType

Implementation of Writer for fd_t

Method write

fn write(&self, v: &[u8])

Method seek

fn seek(&self, _offset: int, _whence: SeekStyle)

Method tell

fn tell(&self) -> uint

Method flush

fn flush(&self) -> int

Method get_type

fn get_type(&self) -> WriterType

Implementation for FdRes

Method new

fn new(fd: fd_t) -> FdRes

Implementation of Drop for FdRes

Method drop

fn drop(&self)

Implementation of WriterUtil for T where <T: Writer>

Method write_char

fn write_char(&self, ch: char)

Method write_str

fn write_str(&self, s: &str)

Method write_line

fn write_line(&self, s: &str)

Method write_int

fn write_int(&self, n: int)

Method write_uint

fn write_uint(&self, n: uint)

Method write_le_uint

fn write_le_uint(&self, n: uint)

Method write_le_int

fn write_le_int(&self, n: int)

Method write_be_uint

fn write_be_uint(&self, n: uint)

Method write_be_int

fn write_be_int(&self, n: int)

Method write_be_u64

fn write_be_u64(&self, n: u64)

Method write_be_u32

fn write_be_u32(&self, n: u32)

Method write_be_u16

fn write_be_u16(&self, n: u16)

Method write_be_i64

fn write_be_i64(&self, n: i64)

Method write_be_i32

fn write_be_i32(&self, n: i32)

Method write_be_i16

fn write_be_i16(&self, n: i16)

Method write_be_f64

fn write_be_f64(&self, f: f64)

Method write_be_f32

fn write_be_f32(&self, f: f32)

Method write_le_u64

fn write_le_u64(&self, n: u64)

Method write_le_u32

fn write_le_u32(&self, n: u32)

Method write_le_u16

fn write_le_u16(&self, n: u16)

Method write_le_i64

fn write_le_i64(&self, n: i64)

Method write_le_i32

fn write_le_i32(&self, n: i32)

Method write_le_i16

fn write_le_i16(&self, n: i16)

Method write_le_f64

fn write_le_f64(&self, f: f64)

Method write_le_f32

fn write_le_f32(&self, f: f32)

Method write_u8

fn write_u8(&self, n: u8)

Method write_i8

fn write_i8(&self, n: i8)

Implementation for BytesWriter

Method new

fn new() -> BytesWriter

Implementation of Writer for BytesWriter

Method write

fn write(&self, v: &[u8])

Method seek

fn seek(&self, offset: int, whence: SeekStyle)

Method tell

fn tell(&self) -> uint

Method flush

fn flush(&self) -> int

Method get_type

fn get_type(&self) -> WriterType

Function FILE_reader

fn FILE_reader(f: *libc::FILE, cleanup: bool) -> @Reader

Function FILE_writer

fn FILE_writer(f: *libc::FILE, cleanup: bool) -> @Writer

Function buffered_file_writer

fn buffered_file_writer(path: &Path) -> Result<@Writer, ~str>

Function fd_writer

fn fd_writer(fd: fd_t, cleanup: bool) -> @Writer

Function file_reader

fn file_reader(path: &Path) -> Result<@Reader, ~str>

Function file_writer

fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str>

Function mk_file_writer

fn mk_file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str>

Function print

fn print(s: &str)

Prints a string to standard output.

This string will not have an implicit newline at the end. If you want an implicit newline, please see println.

Example

// print is imported into the prelude, and so is always available.
print("hello");

Function println

fn println(s: &str)

Prints a string to standard output, followed by a newline.

If you do not want an implicit newline, please see print.

Example

// println is imported into the prelude, and so is always available.
println("hello");

Function read_whole_file

fn read_whole_file(file: &Path) -> Result<~[u8], ~str>

Function read_whole_file_str

fn read_whole_file_str(file: &Path) -> Result<~str, ~str>

Function seek_in_buf

fn seek_in_buf(offset: int, pos: uint, len: uint, whence: SeekStyle) -> uint

Function stderr

fn stderr() -> @Writer

Gives a Writer which allows you to write to standard error.

Example

let stderr = std::io::stderr();
stderr.write_str("hello\\n");

Function stdin

fn stdin() -> @Reader

Gives a Reader that allows you to read values from standard input.

Example

let stdin = std::io::stdin();
let line = stdin.read_line();
std::io::print(line);

Function stdout

fn stdout() -> @Writer

Gives a Writer which allows you to write to the standard output.

Example

let stdout = std::io::stdout();
stdout.write_str("hello\\n");

Function u64_from_be_bytes

fn u64_from_be_bytes(data: &[u8], start: uint, size: uint) -> u64

Function u64_to_be_bytes

fn u64_to_be_bytes<T>(n: u64, size: uint, f: &fn(v: &[u8]) -> T) -> T

Function u64_to_le_bytes

fn u64_to_le_bytes<T>(n: u64, size: uint, f: &fn(v: &[u8]) -> T) -> T

Function with_bytes_reader

fn with_bytes_reader<T>(bytes: &[u8], f: &fn(@Reader) -> T) -> T

Function with_bytes_writer

fn with_bytes_writer(f: &fn(@Writer)) -> ~[u8]

Function with_str_reader

fn with_str_reader<T>(s: &str, f: &fn(@Reader) -> T) -> T

Function with_str_writer

fn with_str_writer(f: &fn(@Writer)) -> ~str