[记]RUST String写入文件


Rust写入文本的格式为&[u8];

需将String转换为&[u8]格式才可写入;

use std::fs;
fn main() {
    // let text = fs::read_to_string(r"C:\Users\Y0137\Desktop\121.txt").unwrap();
    let text = String::from("233");
    fs::write("gg.txt",&mut format!("{}",text).as_bytes()).unwrap();
    let text1 = String::from("244");
    fs::write("ww.txt",&mut text1.as_bytes()).unwrap();
       }

gg.txt

233

 1-------

fn main() {
    let text = String::from("12 23 34 45");
    let vct = text.split_whitespace();
    let mut pp =String::new();
    for t in vct{
        println!("{}",t);
        pp.push_str(t);
    }
    println!("Hello, world!");
    println!("{}",pp);
}
12
23
34
45
Hello, world!
12233445

 2-------

fn main() {
    let text = String::from("12 23 34 45");
    let vct = text.split_whitespace();
    let mut pp = Vec::new();
    for idx in vct{
        pp.push(idx);
    }
    println!("Hello, world!");
    println!("{}",pp[0]);
    println!("{}",pp[pp.len()-1]);
    let mut xeo = String::new();
    for idx in pp{
        xeo.push_str(idx);
    }
    println!("-{}",xeo);
}
Hello, world!
12
45
-12233445