std::io::scannerLine-by-line and word-by-word scanner (bufio.Scanner-style).
Scanner iterates through tokens from a string source, stdin, or a
whole file. The default split function is SplitLines — each call to
scan() advances past one newline-terminated line and returns the
updated scanner state. Use with_split to switch to SplitWords
(whitespace-separated tokens).
import std::io::scanner;
fn main() {
var sc = scanner.from_string("one\ntwo\nthree");
sc = scanner.scan(sc);
while scanner.has_next(sc) {
println(scanner.text(sc));
sc = scanner.scan(sc);
}
}
lines() and words() return a Vec<string> directly:
import std::io::scanner;
fn main() {
let ls = scanner.lines("foo\nbar\nbaz");
for i in 0 .. ls.len() {
println(ls.get(i));
}
}
from_stdin() reads all standard input before scanning. from_file()
returns Result<Scanner, fs.IoError> and reads the whole file before
scanning:
import std::fs;
import std::io::scanner;
fn main() {
match scanner.from_file("input.txt") {
Ok(sc0) => {
var sc = sc0;
sc = scanner.scan(sc);
while scanner.has_next(sc) {
println(scanner.text(sc));
sc = scanner.scan(sc);
}
},
Err(e) => panic(fs.io_error_message(e)),
}
}
| Mode | Description |
|---|---|
SplitLines | Lines split on \n; \r\n stripped (default). |
SplitWords | Fields split on any run of ASCII whitespace. |
from_stringCreate a scanner over s using the default SplitLines mode.
Call scan() to advance to the first token.
import std::io::scanner;
fn main() {
var sc = scanner.from_string("alpha\nbeta\ngamma");
sc = scanner.scan(sc);
while scanner.has_next(sc) {
println(scanner.text(sc));
sc = scanner.scan(sc);
}
}
from_stdinCreate a scanner over all data read from standard input.
This v0.5 implementation reads stdin to completion before scanning, rather
than looping on io.read_line(), so EOF is not confused with a real empty
line.
from_fileRead path into memory and create a scanner over its contents.
Returns Err(fs.IoError) if the file cannot be opened or read.
with_splitOverride the split mode on an existing scanner, resetting to position 0.
Resets the cursor to the start of the source. Call scan() after
with_split to advance to the first token.
import std::io::scanner;
fn main() {
var sc = scanner.from_string("one two three");
sc = scanner.with_split(sc, scanner.SplitWords);
sc = scanner.scan(sc);
while scanner.has_next(sc) {
println(scanner.text(sc));
sc = scanner.scan(sc);
}
}
scanAdvance to the next token and return the updated scanner.
Check has_next() on the returned value; if true, text() gives
the current token. Returns the scanner unchanged once exhausted.
import std::io::scanner;
fn main() {
var sc = scanner.from_string("a\nb\nc");
sc = scanner.scan(sc);
while scanner.has_next(sc) {
println(scanner.text(sc));
sc = scanner.scan(sc);
}
}
next_lineAdvance in line mode and return the updated scanner with the next line.
This helper ignores the current split mode and always uses SplitLines.
A real empty line returns Some(""); EOF returns None.
has_nextReturn true if the last scan() produced a token.
Returns false on a freshly constructed scanner (before the first
scan()) and after exhaustion.
textReturn the current token text.
Valid when has_next() is true; returns an empty string otherwise.
linesReturn all lines from s as a Vec<string>.
Strips \r\n and \n endings. A trailing newline does not produce
a trailing empty element — only content lines are emitted (Go
bufio.Scanner semantics). An empty input returns an empty vector.
For trailing-empty-element behaviour, use std::string.lines().
import std::io::scanner;
fn main() {
let ls = scanner.lines("foo\nbar\n");
// ls == ["foo", "bar"]
println(ls.len());
}
wordsReturn all whitespace-delimited words from s as a Vec<string>.
Any run of ASCII whitespace (space, tab, CR, LF) acts as a separator. Leading and trailing whitespace is ignored.
import std::io::scanner;
fn main() {
let ws = scanner.words(" hello world ");
// ws == ["hello", "world"]
println(ws.len());
}
collectCollect all remaining tokens from sc into a Vec<string>.
If has_next(sc) is already true (i.e. scan() was called before
collect), the current token is included as the first element.
Advances the scanner to exhaustion.
import std::io::scanner;
fn main() {
var sc = scanner.from_string("a\nb\nc\nd");
sc = scanner.scan(sc); // "a"
sc = scanner.scan(sc); // "b"
let rest = scanner.collect(sc);
// rest == ["b", "c", "d"] (current token + remaining)
}
SplitModeHow the scanner tokenises its source.
SplitLinesLines split on \n; a leading \r before \n is stripped.
SplitWordsTokens split on any run of ASCII whitespace (space, tab, \r, \n).
ScannerScanner state value.
Hold a var sc: Scanner and drive it with scan(); see module docs
for full usage.
Byte position of the next unconsumed character.
Text of the current token. Valid after scan() when has_next()
is true; empty string before the first scan() or after exhaustion.
True when the last scan produced a token. Required so a real empty
line ("\n") remains distinguishable from EOF.
True when all tokens have been emitted.
Token split mode; fixed at construction or changed via with_split.