Rust でテキストファイルのレコードを更新


こちらで作成したテキストファイルのレコードを更新します。
Rust でテキストファイルの作成

フォルダー構造

$ tree -L 2
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── text_update.rs
└── target
    └── debug
Cargo.toml
[package]
name = "text_update"
version = "0.1.0"
edition = "2018"

#

[[bin]]
name = "text_update"
path = "src/text_update.rs"

[dependencies]
chrono = "0.4"
src/text_update.rs
// --------------------------------------------------------------------
/*
    src/text_update.rs

                        Jul/17/2020
*/
// --------------------------------------------------------------------
use std::env;
use std::error;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::collections::HashMap;
use std::io::{Write};

use chrono::{Local, Date};
// --------------------------------------------------------------------
fn text_read_proc(fname_in: String) -> HashMap<String,HashMap<String,String>>{
    let mut dict_aa = HashMap::new();
    let file = File::open(fname_in).expect("file not found.");

    for line in BufReader::new(file).lines() {
        let line = line.unwrap();
        let vvv: Vec<&str> = line.trim ().split_terminator('\t').collect();
        let mut unit_aa:HashMap <String,String> = HashMap::new ();
        unit_aa.insert("name".to_string(), vvv[1].to_string());
        unit_aa.insert("population".to_string(), vvv[2].to_string());
        unit_aa.insert("date_mod".to_string(), vvv[3].to_string());

        dict_aa.insert(vvv[0].to_string(), unit_aa);
    }

    dict_aa
}

// --------------------------------------------------------------------
fn text_write_proc (fname_in: String, dict_aa: HashMap<String,HashMap<String,String>>) {
    let mut file = File::create(fname_in).expect("file not found.");

    for (key,value) in dict_aa.iter() {
        let str_out = key.to_string () + "\t" + &value["name"]
            + "\t" + &value["population"]
            + "\t" + &value["date_mod"] + "\n";
        print!("{}", str_out);
        file.write (String::from(str_out).as_bytes()).expect("cannot write.");
    }

}

// --------------------------------------------------------------------
fn main () -> Result<(), Box<dyn error::Error>> {
    eprintln!("*** 開始 ***");

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

    let ref fname_in = args[1];
    let key_in = &args[2];
    let population_in = &args[3];

    println! ("{}",fname_in);
    println! ("{}",key_in);
    println! ("{}",population_in);


    let mut dict_aa = text_read_proc(fname_in.to_string());
// ------------------

    let unit_bb = &dict_aa[key_in];

    println! ("{:?}", unit_bb);
    let name_bb = &unit_bb["name"];
    println! ("{}", name_bb);

    let mut unit_cc:HashMap<String, String> = HashMap::new();
    unit_cc.insert("name".to_string(),name_bb.to_string());
    unit_cc.insert("population".to_string(),population_in.to_string());

    let date_mod: Date<Local> = Local::today();
    println!("{}", date_mod);

    unit_cc.insert("date_mod".to_string(),date_mod.to_string());

    dict_aa.insert(key_in.to_string(),unit_cc);

// ------------------
    text_write_proc (fname_in.to_string(),dict_aa);

    eprintln!("*** 終了 ***");

    Ok(())
}

// --------------------------------------------------------------------

コンパイル

carogo build

実行

cargo run cities.txt t2382  723

ディスクの使用量

$ du --max-depth=2 -BM
1M  ./src
59M ./target/debug
59M ./target
1M  ./.git/refs
1M  ./.git/hooks
1M  ./.git/info
1M  ./.git/objects
1M  ./.git
59M .

再生できるファイルの削除

cargo clean

ディスクの使用量

$ du --max-depth=2 -BM
1M  ./src
1M  ./.git/refs
1M  ./.git/hooks
1M  ./.git/info
1M  ./.git/objects
1M  ./.git
1M  .