LoginSignup
4
5

More than 3 years have passed since last update.

Rustでカレントディレクトリを取得・表示・変更する

Posted at

コード

use std::env;
use std::path::Path;

fn main() -> std::io::Result<()> {
    let path = env::current_dir()?;
    println!("starting dir: {}", path.display());

    let root = Path::new("/");
    assert!(env::set_current_dir(&root).is_ok());

    let path = env::current_dir()?;
    println!("final dir: {}", path.display());

    Ok(())
}

出力

$ rustc wd.rs && ./wd && rm wd
starting dir: /Users/wildmouse/hoge/wd
final dir: /

参考

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5