fn main() {
println!("Hello World");
}
Create a crate
Cleverly (or confusingly ?) call the crate home-dir
.
cargo new home-dir
cd home-dir
Add directories crate as a dependency
The directories crate seems to offer what we need here.
cargo add directories
This will update the Cargo.toml
file:
[package]
name = "home-dir"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
directories = "5.0.1"
Source code
fn main() {
let bd = directories::BaseDirs::new().unwrap();
let home_dir = bd.home_dir();
println!("home_dir: {:?}", home_dir);
}
Running it:
cargo run
home_dir: "/home/gabor"
Indeed that's my home directory on this computer.