LoginSignup
2
4

Rust でテキストファイルの作成

Last updated at Posted at 2020-07-09

Ubuntu 23.10 で Rust のインストール

sudo apt install rustc

インストールされたバージョン

$ rustc --version
rustc 1.75.0 (82e1608df 2023-12-21) (built from a source tarball)
text_create.rs
// --------------------------------------------------------------------
/*
	text_create.rs

						Jul/09/2020
*/
// --------------------------------------------------------------------
use std::env;
use std::fs::File;
use std::io::{Write};

fn main() -> std::io::Result<()> {

eprintln!("*** 開始 ***");

let args: Vec<_> = env::args().collect();

let ref fname_out = args[1];

let mut file = File::create(fname_out)?;
file.write_all(String::from("t2381\t名古屋\t71842\t2005-9-12\n").as_bytes())?;
file.write_all(String::from("t2382\t豊橋\t14278\t2005-3-15\n").as_bytes())?;
file.write_all(String::from("t2383\t岡崎\t65291\t1950-10-2\n").as_bytes())?;
file.write_all(String::from("t2384\t一宮\t31864\t1950-6-22\n").as_bytes())?;
file.write_all(String::from("t2385\t蒲郡\t49158\t1950-8-14\n").as_bytes())?;
eprintln!("*** 終了 ***");
Ok(())

}

// --------------------------------------------------------------------
Makefile
text_create: text_create.rs
	rustc text_create.rs
clean:
	rm -f text_create

コンパイル

$ make
rustc text_create.rs

実行

$ ./text_create cities.txt
*** 開始 ***
*** 終了 ***
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4