Rust ファイル入出力

単純なファイル入出力はstd::fsやstd::ioにある関数を使う
std::fs::read(), std::fs::read_dir(), std::fs::read_to_string(), std::fs_write()

use std::fs;

fn main() {
	let result = fs::read_to_string("sample.txt");
	println!("{:?}", result);
}
use std::fs;

fn main() {
	match fs::read_to_string("sample.txt"){
		Ok(result) => println!("{}", result),
		Err(msg) => println!("エラー:{}", msg),
	}
}
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;

fn main() -> std::io::Result<()> {
	let f = File::open("sample.txt")?;
	let reader = BufReader::new(f);

	for line in reader.lines() {
		println!("{}", line?);
	}
	Ok(())

}
use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> std::io::Result<()> {
	let mut f = File::open("sample.txt")?;
	let mut buffer = [0; 1000];

	let n = f.read(&mut buffer)?;

	println!("{:?}", &buffer[..n]);
	Ok(())
}
use std::fs::File;
use std::io::Write;

fn main() -> std::io::Result<()> {
	let txt = "こんにちは¥nHappy Rust¥n";
	let mut file = File::create("test.txt")?;
	write!(file, "{}", txt)?;
	file.flush()?;
	Ok(())

}
use std::fs::File;
use std::io::{Write, BufWriter};

fn main() -> std::io::Result<()> {
	let file = File::create("sample2.txt")?;
	let mut writer = BufWrite::new(file);
	let data:&str = "Hello¥nHappy Dogs";
	writer.write_all(data.as_bytes())?;
	writer.flush()?;
	Ok(())
}
use qrcode::QrCode;
use image::Luma;

fn main() {
	let code = QrCode::new(b"A0023213").unwrap();
	let image = code.render::<Luma<u8>>().build();

	image.save("sample.png").unwrap();
}