如何在Rust的某个文件夹中创建文件?
我尝试了以下方法:
let f = File::create(Path::new(format!("{}{}","/files/",filename).as_str()));
let f = File::create(format!("{}{}","/files/",filename));
两者都导致:
The system cannot find the path specified. (os error 3)
我设法使用以下命令在与可执行文件相同的目录中创建文件:
let f = File::create(format!("{}",filename));
那么,如何在特定目录中创建文件呢?
ls
在basefolder上:
PS C:\trust\svd2rust\target\debug> ls
Directory: C:\trust\svd2rust\target\debug
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2017-01-24 12:13 .fingerprint
d----- 2017-01-24 12:13 build
d----- 2017-01-26 10:06 deps
d----- 2017-01-24 12:13 examples
d----- 2017-01-26 07:18 files
d----- 2017-01-24 12:13 incremental
d----- 2017-01-24 12:13 native
-a---- 2017-01-24 12:13 0 .cargo-lock
-a---- 2017-01-26 10:06 1079240 libsvd2rust.rlib
-a---- 2017-01-24 21:08 27608 log.rs
-a---- 2017-01-24 13:55 27372 log2.rs
-a---- 2016-08-23 15:07 565888 STM32F401x.svd
-a---- 2016-08-23 15:07 912360 STM32F401xE.svd
-a---- 2016-08-23 15:07 1907985 STM32F40x.svd
-a---- 2017-01-26 10:06 11761561 svd2rust.exe
使用“ files /”而不是“ / files /”解决了该问题。
似乎“ / files /”引用C:\ files的绝对路径,而“ files /”引用可执行文件的相对路径。
因此,当使用“ / files /”时,由于没有文件夹C:\ Files,所以它不起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句