まずサンプル用に文字列をバイトに変換してCursorを生成しました。
Cursorの中身を読み込むにはread_to_end
を使う方法を知りました。read_to_end
ではバイトを取り出せるので、取り出したあとに、String::from_utf8()
を使って文字列に戻すみたいです。(他の方法もあるので後日追記。)
use std::io::Read;
fn main() {
let string = "example";
let mut cursor = std::io::Cursor::new(string.as_bytes());
let mut out = Vec::new();
assert!(cursor.read_to_end(&mut out).is_ok());
assert_eq!(
format!("{}", String::from_utf8(out.to_vec()).unwrap()),
string
);
}