Rust 鏂囦欢璇诲啓

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rust 鏂囦欢璇诲啓相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/cannot' title='cannot'>cannot   stc   mac   not   鑷姩   姝ラ   new   ble   directory   

https://www.twle.cn/c/yufei/rust/rust-basic-file-input-output.html

Rust 鏂囦欢璇诲啓

Rust 鏍囧噯搴撴彁渚涗簡澶ч噺鐨勬ā鍧楀拰鏂规硶鐢ㄤ簬璇诲啓鏂囦欢銆?/p>

Rust 璇█浣跨敤缁撴瀯浣?nbsp;File 鏉ユ弿杩?灞曠幇涓€涓枃浠躲€?/p>

缁撴瀯浣?nbsp;File 鏈夌浉鍏崇殑鎴愬憳鍙橀噺鎴栧嚱鏁扮敤浜庤〃绀虹▼搴忓彲浠ュ鏂囦欢杩涜鐨勬煇浜涙搷浣滃拰鍙敤鐨勬搷浣滄柟娉曘€?/p>

鎵€鏈夊缁撴瀯浣?nbsp;File 鐨勬搷浣滄柟娉曢兘浼氳繑鍥炰竴涓?nbsp;Result 鏋氫妇銆?/p>

涓嬭〃鍒楀嚭浜嗕竴浜涘父鐢ㄧ殑鏂囦欢璇诲啓鏂规硶銆?/p>

妯″潡鏂规硶
鏂规硶绛惧悕
璇存槑
std::fs::File open()
pub fn open(path: P) -> Result
闈欐€佹柟娉曪紝浠?nbsp;鍙 妯″紡鎵撳紑鏂囦欢
std::fs::File create()
pub fn create(path: P) -> Result
闈欐€佹柟娉曪紝浠?nbsp;鍙啓 妯″紡鎵撳紑鏂囦欢銆?br />濡傛灉鏂囦欢瀛樺湪鍒欐竻绌烘棫鍐呭
濡傛灉鏂囦欢涓嶅瓨鍦ㄥ垯鏂板缓
std::fs::remove_file remove_file()
pub fn remove_file(path: P) -> Result<()>
浠庢枃浠剁郴缁熶腑鍒犻櫎鏌愪釜鏂囦欢
std::fs::OpenOptions append()
pub fn append(&mut self, append: bool) -> &mut OpenOptions
璁剧疆鏂囦欢妯″紡涓?nbsp;杩藉姞
std::io::Writes write_all()
fn write_all(&mut self, buf: &[u8]) -> Result<()>
灏?buf 涓殑鎵€鏈夊唴瀹瑰啓鍏ヨ緭鍑烘祦
std::io::Read read_to_string()
fn read_to_string(&mut self, buf: &mut String) -> Result
璇诲彇鎵€鏈夊唴瀹硅浆鎹负瀛楃涓插悗杩藉姞鍒?buf 涓?/td>

Rust 鎵撳紑鏂囦欢

Rust 鏍囧噯搴撲腑鐨?nbsp;std::fs::File 妯″潡鎻愪緵浜嗛潤鎬佹柟娉?nbsp;open() 鐢ㄤ簬鎵撳紑涓€涓枃浠跺苟杩斿洖鏂囦欢鍙ユ焺銆?/p>

open() 鍑芥暟鐨勫師鍨嬪涓?/p>

pub fn open(path: P) -> Result

open() 鍑芥暟鐢ㄤ簬浠ュ彧璇绘ā寮忔墦寮€涓€涓凡缁忓瓨鍦ㄧ殑鏂囦欢锛屽鏋滄枃浠朵笉瀛樺湪锛屽垯浼氭姏鍑轰竴涓敊璇€傚鏋滄枃浠朵笉鍙锛岄偅涔堜篃浼氭姏鍑轰竴涓敊璇€?/p>

鑼冧緥

涓嬮潰鐨勮寖渚嬶紝鎴戜滑浣跨敤 open() 鎵撳紑褰撳墠鐩綍涓嬬殑鏂囦欢锛屽凡缁忔枃浠跺凡缁忓瓨鍦紝鎵€浠ヤ笉浼氭姏鍑轰换浣曢敊璇?/p>

fn main() {
    let file = std::fs::File::open("data.txt").unwrap();
    println!("鏂囦欢鎵撳紑鎴愬姛锛歿:?}",file);
}

缂栬瘧杩愯浠ヤ笂 Rust 浠g爜锛岃緭鍑虹粨鏋滃涓?/p>

鏂囦欢鎵撳紑鎴愬姛锛欶ile { fd: 3, path: "/Users/yufei/Downloads/guess-game-app/src/data.txt", read: true, write: false }

濡傛灉鏂囦欢 data.txt 涓嶅瓨鍦紝鍒欎細鎶涘嚭浠ヤ笅閿欒

thread 鈥榤ain鈥?panicked at 鈥榗alled `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }鈥? src/libcore/result.rs:997:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

Rust 鍒涘缓鏂囦欢

Rust 鏍囧噯搴撲腑鐨?nbsp;std::fs::File 妯″潡鎻愪緵浜嗛潤鎬佹柟娉?nbsp;create() 鐢ㄤ簬鍒涘缓涓€涓枃浠跺苟杩斿洖鍒涘缓鐨勬枃浠跺彞鏌勩€?/p>

create() 鍑芥暟鐨勫師鍨嬪涓?/p>

pub fn create(path: P) -> Result

create() 鍑芥暟鐢ㄤ簬鍒涘缓涓€涓枃浠跺苟杩斿洖鍒涘缓鐨勬枃浠跺彞鏌勩€傚鏋滄枃浠跺凡缁忓瓨鍦紝鍒欎細鍐呴儴璋冪敤 open() 鎵撳紑鏂囦欢銆傚鏋滃垱寤哄け璐ワ紝姣斿鐩綍涓嶅彲鍐欙紝鍒欎細鎶涘嚭閿欒

鑼冧緥

涓嬮潰鐨勪唬鐮侊紝浣跨敤 create() 鍑芥暟鍒涘缓鏂囦欢 data.txt銆?/p>

fn main() {
   let file = std::fs::File::create("data.txt").expect("create failed");
   println!("鏂囦欢鍒涘缓鎴愬姛:{:?}",file);
}

缂栬瘧杩愯浠ヤ笂 Rust 浠g爜锛岃緭鍑虹粨鏋滃涓?/p>

鏂囦欢鍒涘缓鎴愬姛:File { fd: 3, path: "/Users/yufei/Downloads/guess-game-app/src/data.txt", read: false, write: true }

Rust 鍐欏叆鏂囦欢

Rust 璇█鏍囧噯搴?nbsp;std::io::Writes 鎻愪緵浜嗗嚱鏁?nbsp;write_all() 鐢ㄤ簬鍚戣緭鍑烘祦鍐欏叆鍐呭銆?/p>

鍥犱负鏂囦欢娴佷篃鏄緭鍑烘祦鐨勪竴绉嶏紝鎵€浠ヨ鍑芥暟涔熷彲浠ョ敤浜庡悜鏂囦欢鍐欏叆鍐呭銆?/p>

write_all() 鍑芥暟鍦ㄦā鍧?nbsp;std::io::Writes 涓畾涔夛紝瀹冪殑鍑芥暟鍘熷瀷濡備笅

fn write_all(&mut self, buf: &[u8]) -> Result<()>

write_all() 鐢ㄤ簬鍚戝綋鍓嶆祦鍐欏叆 buf 涓殑鍐呭銆傚鏋滃啓鍏ユ垚鍔熷垯杩斿洖鍐欏叆鐨勫瓧鑺傛暟锛屽鏋滃啓鍏ュけ璐ュ垯鎶涘嚭閿欒

鑼冧緥

涓嬮潰鐨勪唬鐮侊紝鎴戜滑浣跨敤 write_all() 鏂规硶鍚戞枃浠?nbsp;data.txt 鍐欏叆涓€浜涘唴瀹?/p>

use std::io::Write;
fn main() {
   let mut file = std::fs::File::create("data.txt").expect("create failed");
   file.write_all("绠€鍗曟暀绋?.as_bytes()).expect("write failed");
   file.write_all("
绠€鍗曠紪绋?.as_bytes()).expect("write failed");
   println!("data written to file" );
}

缂栬瘧杩愯浠ヤ笂 Rust 鑼冧緥锛岃緭鍑虹粨鏋滃涓?/p>

绠€鍗曟暀绋?绠€鍗曠紪绋?

娉ㄦ剰锛?write_all() 鏂规硶骞朵笉浼氬湪鍐欏叆缁撴潫鍚庤嚜鍔ㄥ啓鍏ユ崲琛岀  銆?/p>

Rust 璇诲彇鏂囦欢

Rust 璇诲彇鍐呭鐨勪竴鑸楠や负:

  1. 浣跨敤 open() 鍑芥暟鎵撳紑涓€涓枃浠?/li>
  2. 鐒跺悗浣跨敤 read_to_string() 鍑芥暟浠庢枃浠朵腑璇诲彇鎵€鏈夊唴瀹瑰苟杞崲涓哄瓧绗︿覆銆?/li>

open() 鍑芥暟鎴戜滑鍓嶉潰宸茬粡浠嬬粛杩囦簡锛岃繖娆℃垜浠富瑕佹潵璁茶 read_to_string() 鍑芥暟銆?/p>

read_to_string() 鍑芥暟鐢ㄤ簬浠庝竴涓枃浠朵腑璇诲彇鎵€鏈夊墿浣欑殑鍐呭骞惰浆鎹负瀛楃涓层€?/p>

read_to_string() 鍑芥暟鐨勫師鍨嬪涓?/p>

fn read_to_string(&mut self, buf: &mut String) -> Result

read_to_string() 鍑芥暟鐢ㄤ簬璇诲彇鏂囦欢涓殑鎵€鏈夊唴瀹瑰苟杩藉姞鍒?nbsp;buf 涓紝濡傛灉璇诲彇鎴愬姛鍒欒繑鍥炶鍙栫殑瀛楄妭鏁帮紝濡傛灉璇诲彇澶辫触鍒欐姏鍑洪敊璇€?/p>

鑼冧緥

鎴戜滑棣栧厛鍦ㄥ綋鍓嶇洰褰曚笅鏂板缓涓€涓枃浠?nbsp;data.txt 骞跺啓鍏ヤ互涓嬪唴瀹?/p>

绠€鍗曟暀绋?绠€鍗曠紪绋?

鐒跺悗鎴戜滑鍐欎竴涓皬鑼冧緥锛屼娇鐢?nbsp;read_to_string 鍑芥暟浠庢枃浠朵腑璇诲彇鍐呭銆?/p>

use std::io::Read;

fn main(){
   let mut file = std::fs::File::open("data.txt").unwrap();
   let mut contents = String::new();
   file.read_to_string(&mut contents).unwrap();
   print!("{}", contents);
}

缂栬瘧杩愯浠ヤ笂 Rust 浠g爜锛岃緭鍑虹粨鏋滃涓?/p>

绠€鍗曟暀绋?绠€鍗曠紪绋?

杩藉姞鍐呭鍒版枃浠舵湯灏?/h2>

Rust 鏍稿績鍜屾爣鍑嗗簱骞舵病鏈夋彁渚涚洿鎺ョ殑鍑芥暟鐢ㄤ簬杩藉姞鍐呭鍒版枃浠剁殑鏈熬銆?/p>

浣嗘彁渚涗簡鍑芥暟 append() 鐢ㄤ簬灏嗘枃浠剁殑鎵撳紑妯″紡璁剧疆涓?nbsp;杩藉姞銆?/p>

褰撴枃浠剁殑妯″紡璁剧疆涓?nbsp;杩藉姞 涔嬪悗锛屽啓鍏ユ枃浠剁殑鍐呭灏变笉浼氫唬鏇垮師鍏堢殑鏃у唴瀹硅€屾槸鏀惧湪鏃у唴瀹圭殑鍚庨潰銆?/p>

鍑芥暟 append() 鍦ㄦā鍧?nbsp;std::fs::OpenOptions 涓畾涔夛紝瀹冪殑鍑芥暟鍘熷瀷涓?/p>

pub fn append(&mut self, append: bool) -> &mut OpenOptions

鑼冧緥

涓嬮潰鐨勮寖渚嬶紝鎴戜滑浣跨敤 append() 鏂规硶淇敼鏂囦欢鐨勬墦寮€妯″紡涓?nbsp;杩藉姞銆?/p>

use std::fs::OpenOptions;
use std::io::Write;

fn main() {
   let mut file = OpenOptions::new().append(true).open("data.txt").expect(
      "cannot open file");
   file.write_all("www.twle.cn".as_bytes()).expect("write failed");
   file.write_all("
绠€鍗曟暀绋?.as_bytes()).expect("write failed");
   file.write_all("
绠€鍗曠紪绋?.as_bytes()).expect("write failed");
   println!("鏁版嵁杩藉姞鎴愬姛");
}

缂栬瘧杩愯浠ヤ笂 Rust 浠g爜锛岃緭鍑虹粨鏋滃涓?/p>

鏁版嵁杩藉姞鎴愬姛

鎵撳紑 data.txt 鏂囦欢锛屽彲浠ョ湅鍒板唴瀹瑰涓?/p>

绠€鍗曟暀绋?绠€鍗曠紪绋媤ww.twle.cn
绠€鍗曟暀绋?绠€鍗曠紪绋?

Rust 鍒犻櫎鏂囦欢

Rust 鏍囧噯搴?nbsp;std::fs 鎻愪緵浜嗗嚱鏁?nbsp;remove_file() 鐢ㄤ簬鍒犻櫎鏂囦欢銆?/p>

remove_file() 鍑芥暟鐨勫師鍨嬪涓?/p>

pub fn remove_file<P: AsRef>(path: P) -> Result<()>

娉ㄦ剰锛屽垹闄ゅ彲鑳戒細澶辫触锛屽嵆浣胯繑鍥炵粨鏋滀负 OK锛屼篃鏈夊彲鑳戒笉浼氱珛鍗冲氨鍒犻櫎銆?/p>

鑼冧緥

涓嬮潰鐨勪唬鐮侊紝鎴戜滑浣跨敤 remove_file() 鏂规硶鍒犻櫎 data.txt銆?/p>

expect() 鏂规硶褰撳垹闄ゅ嚭閿欐椂鐢ㄤ簬杈撳嚭鑷畾涔夌殑閿欒娑堟伅銆?/p>

use std::fs;
fn main() {
   fs::remove_file("data.txt").expect("could not remove file");
   println!("file is removed");
}

缂栬瘧杩愯浠ヤ笂鑼冧緥锛岃緭鍑虹粨鏋滃涓?/p>

file is removed

鎵撳紑褰撳墠鐩綍锛屾垜浠彲浠ュ彂鐜版枃浠跺凡缁忚鍒犻櫎浜嗐€?/p>

Rust 澶嶅埗鏂囦欢

Rust 鏍囧噯搴撴病鏈夋彁渚涗换浣曞嚱鏁扮敤浜庡鍒朵竴涓枃浠朵负鍙︿竴涓柊鏂囦欢銆?/p>

浣嗘垜浠彲浠ヤ娇鐢ㄤ笂闈㈡彁鍒扮殑鍑芥暟鍜屾柟娉曟潵瀹炵幇鏂囦欢鐨勫鍒跺姛鑳姐€?/p>

涓嬮潰鐨勪唬鐮侊紝鎴戜滑妯′豢绠€鍗曠増鏈殑 copy 鍛戒护

copy old_file_name  new_file_name

鍏蜂綋鐨?Rust 浠g爜濡備笅

use std::io::Read;
use std::io::Write;

fn main() {
   let mut command_line: std::env::Args = std::env::args();
   command_line.next().unwrap();

   // 璺宠繃绋嬪簭鍚?   // 鍘熸枃浠?   let source = command_line.next().unwrap();

   // 鏂版枃浠?   let destination = command_line.next().unwrap();
   let mut file_in = std::fs::File::open(source).unwrap();
   let mut file_out = std::fs::File::create(destination).unwrap();
   let mut buffer = [0u8; 4096];
   loop {
      let nbytes = file_in.read(&mut buffer).unwrap();
      file_out.write(&buffer[..nbytes]).unwrap();
      if nbytes < buffer.len() { break; }
   }
}

棣栧厛缂栬瘧涓婇潰鐨勪唬鐮?/p>

[www.twle.cn]$ rustc main.rs 

鐒跺悗浣跨敤涓嬮潰鐨勫懡浠ゆ潵杩愯

  1. windows 鐢佃剳

    [www.twle.cn]$ ./main.exe data.txt data_new.txt
    
  2. linux / mac 鐢佃剳

    [www.twle.cn]$ ./main data.txt data_new.txt
    

鍏朵腑

  • data.txt 涓烘垜浠兂瑕佸鍒剁殑鍘熸枃浠惰矾寰?/li>
  • data_new.txt 涓烘垜浠兂瑕佺殑鏂版枃浠惰矾寰?/li>

以上是关于Rust 鏂囦欢璇诲啓的主要内容,如果未能解决你的问题,请参考以下文章

閫氳繃`RestTemplate`涓婁紶鏂囦欢(InputStreamResource璇﹁В)

MATLAB璇诲彇鏂囦欢鈥斺€斾粠闈炲父瑙勬枃鏈枃浠朵腑璇诲彇鏁版嵁

sqlserver璇诲彇excel鏂囦欢鏁版嵁鍒版暟鎹簱

NO.36 璇诲啓鍒嗙:CopyOnWrite鏈哄埗

MyCat瀹炵幇鏁版嵁搴撲笌鏁版嵁搴撲箣闂寸殑璇诲啓鍒嗙

odoo 瀵煎叆鏂囦欢xml鍒朵綔,浣跨敤csv 鏂囦欢鍒朵綔xml 鏂囦欢