9 个版本
使用旧的 Rust 2015
0.1.8 | 2018年1月2日 |
---|---|
0.1.7 | 2018年1月2日 |
754 在 文件系统
159 每月下载量
26KB
476 行
char_stream
Rust 语言统一字符读取接口,支持 str, String, bytes, File 和 Stdin。
安装
在 Cargo.toml 中
[dependencies]
char_stream = "*"
示例
对于 str
use char_stream::CharStream;
let mut stream = CharStream::from("Hello 世界❤");
assert_eq!('H', stream.next().unwrap());
assert_eq!('e', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('o', stream.next().unwrap());
assert_eq!(' ', stream.next().unwrap());
assert_eq!('世', stream.next().unwrap());
assert_eq!('界', stream.next().unwrap());
assert_eq!('❤', stream.next().unwrap());
assert_eq!(None, stream.next());
对于 String
use char_stream::CharStream;
let s = String::from("Hello 世界❤");
let mut stream = CharStream::from_string(s);
assert_eq!('H', stream.next().unwrap());
assert_eq!('e', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('o', stream.next().unwrap());
assert_eq!(' ', stream.next().unwrap());
assert_eq!('世', stream.next().unwrap());
assert_eq!('界', stream.next().unwrap());
assert_eq!('❤', stream.next().unwrap());
assert_eq!(None, stream.next());
对于 bytes
use char_stream::CharStream;
let bytes: [u8; 15] = [72, 101, 108, 108, 111, 32, 228, 184, 150, 231, 149, 140, 226, 157, 164];
if let Ok(mut stream) = CharStream::from_bytes(&bytes) {
assert_eq!('H', stream.next().unwrap());
assert_eq!('e', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('o', stream.next().unwrap());
assert_eq!(' ', stream.next().unwrap());
assert_eq!('世', stream.next().unwrap());
assert_eq!('界', stream.next().unwrap());
assert_eq!('❤', stream.next().unwrap());
assert_eq!(None, stream.next());
}
对于 File
extern crate tempfile;
extern crate char_stream;
use std::io::prelude::*;
use std::io::{Seek, SeekFrom};
use std::fs::File;
use char_stream::CharStream;
fn main(){
let test_data = "Hello\n 世界❤";
// write test data to tempfile
let mut tmpfile: File = tempfile::tempfile().unwrap();
tmpfile.write_all(test_data.as_bytes()).unwrap();
// Seek to start
tmpfile.seek(SeekFrom::Start(0)).unwrap();
// read test data from tempfile
let mut stream = CharStream::from_file(tmpfile);
assert_eq!('H', stream.next().unwrap());
assert_eq!('e', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('o', stream.next().unwrap());
assert_eq!('\n', stream.next().unwrap());
assert_eq!(' ', stream.next().unwrap());
assert_eq!('世', stream.next().unwrap());
assert_eq!('界', stream.next().unwrap());
assert_eq!('❤', stream.next().unwrap());
assert_eq!(None, stream.next());
}
对于 Stdin
extern crate char_stream;
use char_stream::CharStream;
fn main() {
let mut stream = CharStream::from_stdin();
while let Some(ch) = stream.next() {
println!("ch: {}", ch);
}
}
反向 str
extern crate char_stream;
use char_stream::CharStream;
fn main() {
let input = "stressed";
let stream = CharStream::from(input);
let rev_stream = stream.wend_iter().rev();
let result: String = rev_stream.collect();
println!("'{}' reverse to '{}'", input , result);
}