The io
module contains basic input and output routines.
A quick summary:
Reader
and Writer
traitsThese traits define the minimal set of methods that anything that can do input and output should implement.
ReaderUtil
and WriterUtil
traitsRicher 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.
print
and println
These very useful functions are defined here. You generally don't need to import them, though, as the prelude already does.
stdin
, stdout
, and stderr
These functions return references to the classic three file descriptors. They implement Reader
and Writer
, where appropriate.
fd_t
FileFlag
SeekStyle
- The SeekStyle enum describes the relationship between the position we'd like to seek to from our current positionWriterType
BytesReader
BytesWriter
FILERes
FdRes
Reader
- The core Reader traitReaderUtil
- The ReaderUtil
trait is a home for many of the utility functions a particular Reader should implement.Writer
- The raw underlying writer traitWriterUtil
- Generic utility functions defined on writers.of Reader for @Reader
of ReaderUtil for T where <T: Reader>
of Reader for *libc::FILE
of Reader for Wrapper<R, C> where <R: Reader, C>
for FILERes
of Drop for FILERes
of Reader for BytesReader
of ::std::cmp::Eq for WriterType
- Automatically derived.of Writer for @Writer
of Writer for Wrapper<W, C> where <W: Writer, C>
of Writer for *libc::FILE
of Writer for fd_t
for FdRes
of Drop for FdRes
of WriterUtil for T where <T: Writer>
for BytesWriter
of Writer for BytesWriter
FILE_reader
FILE_writer
buffered_file_writer
fd_writer
file_reader
file_writer
mk_file_writer
print
- Prints a string to standard output.println
- Prints a string to standard output, followed by a newline.read_whole_file
read_whole_file_str
seek_in_buf
stderr
- Gives a Writer
which allows you to write to standard error.stdin
- Gives a Reader
that allows you to read values from standard input.stdout
- Gives a Writer
which allows you to write to the standard output.u64_from_be_bytes
u64_to_be_bytes
u64_to_le_bytes
with_bytes_reader
with_bytes_writer
with_str_reader
with_str_writer
io::fsync
io::rustrt
fd_t
type fd_t = c_int
FileFlag
Append
Create
Truncate
NoFlag
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:
SeekSet
means that the new position should become our position.SeekCur
means that we should seek from the current position.SeekEnd
means that we should seek from the end.None right now.
SeekSet
SeekEnd
SeekCur
WriterType
Screen
File
BytesReader
pub struct BytesReader {
bytes: &'static [u8],
pos: @mut uint,
}
BytesWriter
pub struct BytesWriter {
bytes: @mut ~[u8],
pos: @mut uint,
}
FILERes
pub struct FILERes {
f: *libc::FILE,
}
FdRes
pub struct FdRes {
fd: fd_t,
}
Reader
The core Reader trait. All readers must implement this trait.
None right now.
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.
None right now.
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.
None right now.
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.
None right now.
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.
None right now.
tell
fn tell(&self) -> uint
Returns the current position within the stream.
tell
is conceptually similar to C's ftell
function.
None right now.
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.'
None right now.
read_bytes
fn read_bytes(&self, len: uint) -> ~[u8]
Reads len
number of bytes, and gives you a new vector back.
None right now.
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.
None right now.
read_line
fn read_line(&self) -> ~str
Reads up until the first '\n' or EOF.
The '\n' is not included in the result.
None right now.
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.
None right now.
read_char
fn read_char(&self) -> char
Reads a single UTF-8 encoded char.
None right now.
read_c_str
fn read_c_str(&self) -> ~str
Reads up until the first null byte or EOF.
The null byte is not returned.
None right now.
read_whole_stream
fn read_whole_stream(&self) -> ~[u8]
Reads all remaining data in the stream.
None right now.
each_byte
fn each_byte(&self, it: &fn(int) -> bool) -> bool
Iterate over every byte until EOF or the iterator breaks.
None right now.
each_char
fn each_char(&self, it: &fn(char) -> bool) -> bool
Iterate over every char until EOF or the iterator breaks.
None right now.
each_line
fn each_line(&self, it: &fn(&str) -> bool) -> bool
Iterate over every line until EOF or the iterator breaks.
None right now.
read_lines
fn read_lines(&self) -> ~[~str]
Reads all of the lines in the stream.
Returns a vector of those lines.
None right now.
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.
None right now.
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.
None right now.
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.
None right now.
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.
None right now.
read_le_uint
fn read_le_uint(&self) -> uint
Reads a little-endian unsigned integer.
The number of bytes returned is system-dependant.
None right now.
read_le_int
fn read_le_int(&self) -> int
Reads a little-endian integer.
The number of bytes returned is system-dependant.
None right now.
read_be_uint
fn read_be_uint(&self) -> uint
Reads a big-endian unsigned integer.
The number of bytes returned is system-dependant.
None right now.
read_be_int
fn read_be_int(&self) -> int
Reads a big-endian integer.
The number of bytes returned is system-dependant.
None right now.
read_be_u64
fn read_be_u64(&self) -> u64
Reads a big-endian u64
.
u64
s are 8 bytes long.
None right now.
read_be_u32
fn read_be_u32(&self) -> u32
Reads a big-endian u32
.
u32
s are 4 bytes long.
None right now.
read_be_u16
fn read_be_u16(&self) -> u16
Reads a big-endian u16
.
u16
s are 2 bytes long.
None right now.
read_be_i64
fn read_be_i64(&self) -> i64
Reads a big-endian i64
.
i64
s are 8 bytes long.
None right now.
read_be_i32
fn read_be_i32(&self) -> i32
Reads a big-endian i32
.
i32
s are 4 bytes long.
None right now.
read_be_i16
fn read_be_i16(&self) -> i16
Reads a big-endian i16
.
i16
s are 2 bytes long.
None right now.
read_be_f64
fn read_be_f64(&self) -> f64
Reads a big-endian f64
.
f64
s are 8 byte, IEEE754 double-precision floating point numbers.
None right now.
read_be_f32
fn read_be_f32(&self) -> f32
Reads a big-endian f32
.
f32
s are 4 byte, IEEE754 single-precision floating point numbers.
None right now.
read_le_u64
fn read_le_u64(&self) -> u64
Reads a little-endian u64
.
u64
s are 8 bytes long.
None right now.
read_le_u32
fn read_le_u32(&self) -> u32
Reads a little-endian u32
.
u32
s are 4 bytes long.
None right now.
read_le_u16
fn read_le_u16(&self) -> u16
Reads a little-endian u16
.
u16
s are 2 bytes long.
None right now.
read_le_i64
fn read_le_i64(&self) -> i64
Reads a little-endian i64
.
i64
s are 8 bytes long.
None right now.
read_le_i32
fn read_le_i32(&self) -> i32
Reads a little-endian i32
.
i32
s are 4 bytes long.
None right now.
read_le_i16
fn read_le_i16(&self) -> i16
Reads a little-endian i16
.
i16
s are 2 bytes long.
None right now.
read_le_f64
fn read_le_f64(&self) -> f64
Reads a little-endian f64
.
f64
s are 8 byte, IEEE754 double-precision floating point numbers.
None right now.
read_le_f32
fn read_le_f32(&self) -> f32
Reads a little-endian f32
.
f32
s are 4 byte, IEEE754 single-precision floating point numbers.
None right now.
read_u8
fn read_u8(&self) -> u8
Read a u8.
u8
s are 1 byte.
None right now.
read_i8
fn read_i8(&self) -> i8
Read an i8.
i8
s are 1 byte.
None right now.
Writer
The raw underlying writer trait. All writers must implement this.
write
fn write(&self, v: &[u8])
Write all of the given bytes.
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.
tell
fn tell(&self) -> uint
Return the current position within the stream.
flush
fn flush(&self) -> int
Flush the output buffer for this stream (if there is one).
get_type
fn get_type(&self) -> WriterType
Determine if this Writer is writing to a file or not.
WriterUtil
Generic utility functions defined on writers.
write_char
fn write_char(&self, ch: char)
Write a single utf-8 encoded char.
write_str
fn write_str(&self, s: &str)
Write every char in the given str, encoded as utf-8.
write_line
fn write_line(&self, s: &str)
Write the given str, as utf-8, followed by '\n'.
write_int
fn write_int(&self, n: int)
Write the result of passing n through int::to_str_bytes
.
write_uint
fn write_uint(&self, n: uint)
Write the result of passing n through uint::to_str_bytes
.
write_le_uint
fn write_le_uint(&self, n: uint)
Write a little-endian uint (number of bytes depends on system).
write_le_int
fn write_le_int(&self, n: int)
Write a little-endian int (number of bytes depends on system).
write_be_uint
fn write_be_uint(&self, n: uint)
Write a big-endian uint (number of bytes depends on system).
write_be_int
fn write_be_int(&self, n: int)
Write a big-endian int (number of bytes depends on system).
write_be_u64
fn write_be_u64(&self, n: u64)
Write a big-endian u64 (8 bytes).
write_be_u32
fn write_be_u32(&self, n: u32)
Write a big-endian u32 (4 bytes).
write_be_u16
fn write_be_u16(&self, n: u16)
Write a big-endian u16 (2 bytes).
write_be_i64
fn write_be_i64(&self, n: i64)
Write a big-endian i64 (8 bytes).
write_be_i32
fn write_be_i32(&self, n: i32)
Write a big-endian i32 (4 bytes).
write_be_i16
fn write_be_i16(&self, n: i16)
Write a big-endian i16 (2 bytes).
write_be_f64
fn write_be_f64(&self, f: f64)
Write a big-endian IEEE754 double-precision floating-point (8 bytes).
write_be_f32
fn write_be_f32(&self, f: f32)
Write a big-endian IEEE754 single-precision floating-point (4 bytes).
write_le_u64
fn write_le_u64(&self, n: u64)
Write a little-endian u64 (8 bytes).
write_le_u32
fn write_le_u32(&self, n: u32)
Write a little-endian u32 (4 bytes).
write_le_u16
fn write_le_u16(&self, n: u16)
Write a little-endian u16 (2 bytes).
write_le_i64
fn write_le_i64(&self, n: i64)
Write a little-endian i64 (8 bytes).
write_le_i32
fn write_le_i32(&self, n: i32)
Write a little-endian i32 (4 bytes).
write_le_i16
fn write_le_i16(&self, n: i16)
Write a little-endian i16 (2 bytes).
write_le_f64
fn write_le_f64(&self, f: f64)
Write a little-endian IEEE754 double-precision floating-point (8 bytes).
write_le_f32
fn write_le_f32(&self, f: f32)
Write a little-endian IEEE754 single-precision floating-point (4 bytes).
write_u8
fn write_u8(&self, n: u8)
Write a u8 (1 byte).
write_i8
fn write_i8(&self, n: i8)
Write a i8 (1 byte).
Reader
for @Reader
read
fn read(&self, bytes: &mut [u8], len: uint) -> uint
read_byte
fn read_byte(&self) -> int
eof
fn eof(&self) -> bool
seek
fn seek(&self, position: int, style: SeekStyle)
tell
fn tell(&self) -> uint
ReaderUtil
for T
where <T: Reader>
read_bytes
fn read_bytes(&self, len: uint) -> ~[u8]
read_until
fn read_until(&self, c: u8, include: bool) -> ~str
read_line
fn read_line(&self) -> ~str
read_chars
fn read_chars(&self, n: uint) -> ~[char]
read_char
fn read_char(&self) -> char
read_c_str
fn read_c_str(&self) -> ~str
read_whole_stream
fn read_whole_stream(&self) -> ~[u8]
each_byte
fn each_byte(&self, it: &fn(int) -> bool) -> bool
each_char
fn each_char(&self, it: &fn(char) -> bool) -> bool
each_line
fn each_line(&self, it: &fn(s: &str) -> bool) -> bool
read_lines
fn read_lines(&self) -> ~[~str]
read_le_uint_n
fn read_le_uint_n(&self, nbytes: uint) -> u64
read_le_int_n
fn read_le_int_n(&self, nbytes: uint) -> i64
read_be_uint_n
fn read_be_uint_n(&self, nbytes: uint) -> u64
read_be_int_n
fn read_be_int_n(&self, nbytes: uint) -> i64
read_le_uint
fn read_le_uint(&self) -> uint
read_le_int
fn read_le_int(&self) -> int
read_be_uint
fn read_be_uint(&self) -> uint
read_be_int
fn read_be_int(&self) -> int
read_be_u64
fn read_be_u64(&self) -> u64
read_be_u32
fn read_be_u32(&self) -> u32
read_be_u16
fn read_be_u16(&self) -> u16
read_be_i64
fn read_be_i64(&self) -> i64
read_be_i32
fn read_be_i32(&self) -> i32
read_be_i16
fn read_be_i16(&self) -> i16
read_be_f64
fn read_be_f64(&self) -> f64
read_be_f32
fn read_be_f32(&self) -> f32
read_le_u64
fn read_le_u64(&self) -> u64
read_le_u32
fn read_le_u32(&self) -> u32
read_le_u16
fn read_le_u16(&self) -> u16
read_le_i64
fn read_le_i64(&self) -> i64
read_le_i32
fn read_le_i32(&self) -> i32
read_le_i16
fn read_le_i16(&self) -> i16
read_le_f64
fn read_le_f64(&self) -> f64
read_le_f32
fn read_le_f32(&self) -> f32
read_u8
fn read_u8(&self) -> u8
read_i8
fn read_i8(&self) -> i8
Reader
for *libc::FILE
read
fn read(&self, bytes: &mut [u8], len: uint) -> uint
read_byte
fn read_byte(&self) -> int
eof
fn eof(&self) -> bool
seek
fn seek(&self, offset: int, whence: SeekStyle)
tell
fn tell(&self) -> uint
Reader
for Wrapper<R, C>
where <R: Reader, C>
read
fn read(&self, bytes: &mut [u8], len: uint) -> uint
read_byte
fn read_byte(&self) -> int
eof
fn eof(&self) -> bool
seek
fn seek(&self, off: int, whence: SeekStyle)
tell
fn tell(&self) -> uint
FILERes
new
fn new(f: *libc::FILE) -> FILERes
Drop
for FILERes
drop
fn drop(&self)
Reader
for BytesReader
read
fn read(&self, bytes: &mut [u8], len: uint) -> uint
read_byte
fn read_byte(&self) -> int
eof
fn eof(&self) -> bool
seek
fn seek(&self, offset: int, whence: SeekStyle)
tell
fn tell(&self) -> uint
::std::cmp::Eq
for WriterType
Automatically derived.
eq
fn eq(&self, __arg_0: &WriterType) -> ::bool
ne
fn ne(&self, __arg_0: &WriterType) -> ::bool
Writer
for @Writer
write
fn write(&self, v: &[u8])
seek
fn seek(&self, a: int, b: SeekStyle)
tell
fn tell(&self) -> uint
flush
fn flush(&self) -> int
get_type
fn get_type(&self) -> WriterType
Writer
for Wrapper<W, C>
where <W: Writer, C>
write
fn write(&self, bs: &[u8])
seek
fn seek(&self, off: int, style: SeekStyle)
tell
fn tell(&self) -> uint
flush
fn flush(&self) -> int
get_type
fn get_type(&self) -> WriterType
Writer
for *libc::FILE
write
fn write(&self, v: &[u8])
seek
fn seek(&self, offset: int, whence: SeekStyle)
tell
fn tell(&self) -> uint
flush
fn flush(&self) -> int
get_type
fn get_type(&self) -> WriterType
Writer
for fd_t
write
fn write(&self, v: &[u8])
seek
fn seek(&self, _offset: int, _whence: SeekStyle)
tell
fn tell(&self) -> uint
flush
fn flush(&self) -> int
get_type
fn get_type(&self) -> WriterType
FdRes
new
fn new(fd: fd_t) -> FdRes
Drop
for FdRes
drop
fn drop(&self)
WriterUtil
for T
where <T: Writer>
write_char
fn write_char(&self, ch: char)
write_str
fn write_str(&self, s: &str)
write_line
fn write_line(&self, s: &str)
write_int
fn write_int(&self, n: int)
write_uint
fn write_uint(&self, n: uint)
write_le_uint
fn write_le_uint(&self, n: uint)
write_le_int
fn write_le_int(&self, n: int)
write_be_uint
fn write_be_uint(&self, n: uint)
write_be_int
fn write_be_int(&self, n: int)
write_be_u64
fn write_be_u64(&self, n: u64)
write_be_u32
fn write_be_u32(&self, n: u32)
write_be_u16
fn write_be_u16(&self, n: u16)
write_be_i64
fn write_be_i64(&self, n: i64)
write_be_i32
fn write_be_i32(&self, n: i32)
write_be_i16
fn write_be_i16(&self, n: i16)
write_be_f64
fn write_be_f64(&self, f: f64)
write_be_f32
fn write_be_f32(&self, f: f32)
write_le_u64
fn write_le_u64(&self, n: u64)
write_le_u32
fn write_le_u32(&self, n: u32)
write_le_u16
fn write_le_u16(&self, n: u16)
write_le_i64
fn write_le_i64(&self, n: i64)
write_le_i32
fn write_le_i32(&self, n: i32)
write_le_i16
fn write_le_i16(&self, n: i16)
write_le_f64
fn write_le_f64(&self, f: f64)
write_le_f32
fn write_le_f32(&self, f: f32)
write_u8
fn write_u8(&self, n: u8)
write_i8
fn write_i8(&self, n: i8)
BytesWriter
new
fn new() -> BytesWriter
Writer
for BytesWriter
write
fn write(&self, v: &[u8])
seek
fn seek(&self, offset: int, whence: SeekStyle)
tell
fn tell(&self) -> uint
flush
fn flush(&self) -> int
get_type
fn get_type(&self) -> WriterType
FILE_reader
fn FILE_reader(f: *libc::FILE, cleanup: bool) -> @Reader
FILE_writer
fn FILE_writer(f: *libc::FILE, cleanup: bool) -> @Writer
buffered_file_writer
fn buffered_file_writer(path: &Path) -> Result<@Writer, ~str>
fd_writer
fn fd_writer(fd: fd_t, cleanup: bool) -> @Writer
file_reader
fn file_reader(path: &Path) -> Result<@Reader, ~str>
file_writer
fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str>
mk_file_writer
fn mk_file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str>
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
.
// print is imported into the prelude, and so is always available.
print("hello");
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
.
// println is imported into the prelude, and so is always available.
println("hello");
read_whole_file
fn read_whole_file(file: &Path) -> Result<~[u8], ~str>
read_whole_file_str
fn read_whole_file_str(file: &Path) -> Result<~str, ~str>
seek_in_buf
fn seek_in_buf(offset: int, pos: uint, len: uint, whence: SeekStyle) -> uint
stderr
fn stderr() -> @Writer
Gives a Writer
which allows you to write to standard error.
let stderr = std::io::stderr();
stderr.write_str("hello\\n");
stdin
fn stdin() -> @Reader
Gives a Reader
that allows you to read values from standard input.
let stdin = std::io::stdin();
let line = stdin.read_line();
std::io::print(line);
stdout
fn stdout() -> @Writer
Gives a Writer
which allows you to write to the standard output.
let stdout = std::io::stdout();
stdout.write_str("hello\\n");
u64_from_be_bytes
fn u64_from_be_bytes(data: &[u8], start: uint, size: uint) -> u64
u64_to_be_bytes
fn u64_to_be_bytes<T>(n: u64, size: uint, f: &fn(v: &[u8]) -> T) -> T
u64_to_le_bytes
fn u64_to_le_bytes<T>(n: u64, size: uint, f: &fn(v: &[u8]) -> T) -> T
with_bytes_reader
fn with_bytes_reader<T>(bytes: &[u8], f: &fn(@Reader) -> T) -> T
with_bytes_writer
fn with_bytes_writer(f: &fn(@Writer)) -> ~[u8]
with_str_reader
fn with_str_reader<T>(s: &str, f: &fn(@Reader) -> T) -> T
with_str_writer
fn with_str_writer(f: &fn(@Writer)) -> ~str