Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get path.join in Rust?

Tags:

node.js

rust

How do I write the path.join in Rust. I tried multiple examples but couldn't get it.

const exeDirectory = path.join(__dirname, '..', 'bin', 'openvpn.exe');
const processFile = path.join(__dirname, '..', '1');

I want to convert these lines of JS into Rust.

like image 731
Saadullah Avatar asked Sep 08 '25 00:09

Saadullah


2 Answers

Use Path which has the .join method

Path::new("..").join("bin").join("openvpn.exe");
like image 66
transistor Avatar answered Sep 09 '25 13:09

transistor


Another option is collecting an iterator of string into a PathBuf:

let path: PathBuf = ["..", "bin", "openvpn.exe"].iter().collect();

This is equivalent to creating a new PathBuf and calling .push() for each string in the iterator. To add multiple new components to an existing PathBuf, you can use the extend() method:

let mut path = PathBuf::from(dir_name);
path.extend(&["..", "bin", "openvpn.exe"]);
like image 32
Sven Marnach Avatar answered Sep 09 '25 14:09

Sven Marnach