Skip to content

Instantly share code, notes, and snippets.

@segfo
Last active October 31, 2017 13:35
Show Gist options
  • Save segfo/36cbdd770298cd2f4f23f028d18be446 to your computer and use it in GitHub Desktop.
Save segfo/36cbdd770298cd2f4f23f028d18be446 to your computer and use it in GitHub Desktop.
部分的にファイルを読み取る
use std::fs::File;
use std::io::prelude::*;
fn main(){
let mut file = File::open("src/main.rs").unwrap();
// ベクタを使ってread_to_endしているのはヒープに直接ぶち込みたいから。
// スタックに制約がない場合は、普通の配列を初期化してでもOK
// 5回繰り返す。
for i in 0..5{
let mut buf = Vec::<u8>::new();
// 参照でやるとmoveが起こらないらしい。わからん。
let s=(&file).take(7).read_to_end(&mut buf).unwrap();
println!("{:?} / {}",buf,s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment