[记]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
---------------------------------------------
use std::fs; use std::io::{Write}; fn main(){ let mut file = fs::OpenOptions::new().write(true).append(true).create(true).open("test.txt").unwrap(); let sstr = String::from("233Test"); // fs::write("test.txt",sstr.as_bytes()); // fs::write("test.txt",sstr.as_bytes()); file.write_all(sstr.as_bytes()).unwrap(); file.write_all(sstr.as_bytes()).unwrap(); }
----------------------------------------------------------------
use std::fs; use std::io::{Read}; fn main(){ let mut file = fs::OpenOptions::new().read(true).append(true).create(true).open("test.txt").unwrap(); let mut getstr = String::new(); file.read_to_string(&mut getstr).unwrap(); let xe = getstr.replace("\r", ""); let xee:Vec<&str> = xe.split("\n").collect(); println!("{}",xee.len()); for idx in xee{ println!("{}",idx); } }
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
字符串处理相关----
fn main(){ let will_deal = String::from("2=1+1\n3=1+2\n4=2+2"); struct Tstruct{//2=1+1 item1:String,//2 itemn:Vec<String>//1,1 } let mut xe:Vec<Tstruct>=Vec::new(); let first_deal = will_deal.split_whitespace().map(|x| x.to_string()); let mut t_deal_1:Vec<String> =Vec::new(); let mut secend_deal:Vec<String>=Vec::new(); let mut t_deal_2:Vec<Vec<String>>=Vec::new(); for idx in first_deal{ let datat:Vec<String> = deal1(idx); let datat1:String =datat[0].clone(); let datat2:String = datat[1].clone(); t_deal_1.push(datat1); secend_deal.push(datat2); } for idy in secend_deal{ let datatt:Vec<String> = deal2(idy); t_deal_2.push(datatt); } for idxy in 0..t_deal_1.len(){ let store = Tstruct{item1:t_deal_1[idxy].clone(),itemn:t_deal_2[idxy].clone()}; xe.push(store); } for idx in xe{ println!("item1"); println!("{}",idx.item1); for idxy in idx.itemn{ println!("itemn"); println!("{}",idxy); } } } fn deal1(datatt:String)->Vec<String>{ let dataxa = datatt.split("=").map(|x| x.to_string()).collect(); dataxa } fn deal2(datatt:String)->Vec<String>{ let dataxa = datatt.split("+").map(|x| x.to_string()).collect(); dataxa }
-----update---
fn main(){ let will_deal = String::from("2=1+1\n3=1+2\n4=2+2"); struct Tstruct{//2=1+1 item1:String,//2 itemn:Vec<String>//1,1 } impl Tstruct{ pub fn create_key(key_d:String)->Self{ let allhead:Vec<String> = key_d.split("=").map(|xs| xs.to_string()).collect(); let head = allhead[0].clone(); let body:Vec<String> = allhead[1].clone().split("+").map(|xx| xx.to_string()).collect(); Tstruct{ item1:head, itemn:body } } } let getdata:Vec<Tstruct> = will_deal.split_whitespace().map(|xa| Tstruct::create_key(xa.to_string())).collect(); for idx in getdata{ println!("head"); println!("{}",idx.item1); println!("body"); for idy in idx.itemn{ println!("{}",idy); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!