Dzwebs.Net

撰写电脑技术杂文十余年

rust之PathBuf获取默认根目录及添加目录和获取某文件路径

Admin | 2023-3-12 16:05:06 | 被阅次数 | 1367

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  如何通过rust表示路径及修改路径获取某文件的路径?代码如下:

  use std::path::PathBuf;
  fn main(){

//env!("CARGO_MANIFEST_DIR")默认获取Cargo.toml文件所在的位置
let mut config_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
println!("config file: {:?}", config_path);
        config_path.push("src");
println!("config file: {:?}", config_path);
config_path.push("images");
println!("config file: {:?}", config_path);
       config_path.push("123.png");
       println!("config file: {:?}", config_path);
}

   /*输出如下内容
config file: "D:\\MyRustProject\\test1"
config file: "D:\\MyRustProject\\test1\\src"
config file: "D:\\MyRustProject\\test1\\src\\images"
config file: "D:\\MyRustProject\\test1\\src\\images\\123.png"
*/


该杂文来自: 最新技术

上一篇:struct之impl下的from.into和new方法

下一篇:rust之impl Mutex impl struct tokio async await

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计