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 璇诲彇鍐呭鐨勪竴鑸楠や负:
- 浣跨敤
open()
鍑芥暟鎵撳紑涓€涓枃浠?/li> - 鐒跺悗浣跨敤
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
鐒跺悗浣跨敤涓嬮潰鐨勫懡浠ゆ潵杩愯
-
windows 鐢佃剳
[www.twle.cn]$ ./main.exe data.txt data_new.txt
-
linux / mac 鐢佃剳
[www.twle.cn]$ ./main data.txt data_new.txt
鍏朵腑
data.txt
涓烘垜浠兂瑕佸鍒剁殑鍘熸枃浠惰矾寰?/li>data_new.txt
涓烘垜浠兂瑕佺殑鏂版枃浠惰矾寰?/li>
以上是关于Rust 鏂囦欢璇诲啓的主要内容,如果未能解决你的问题,请参考以下文章
閫氳繃`RestTemplate`涓婁紶鏂囦欢(InputStreamResource璇﹁В)