As a Rust beginner, I would like to know how I could improve the following points considering the function below:
- Handling
Path
andPathBuf
types correctly; - Performance;
- Rust writing style in general.
Here is the function, decorated with a full working example:
use std::env;
use std::path::{Path, PathBuf};
const RUSV_FILENAME: &str = ".rusv.json";
/**
* Find a rusv file in the current or parent directories of the given directory.
*/
fn find_rusv_file(starting_directory: &Path) -> Option<PathBuf> {
let mut directory = starting_directory;
let rusv_filename = Path::new(&RUSV_FILENAME);
loop {
let filepath: PathBuf = [
directory,
rusv_filename
].iter().collect();
if filepath.is_file() {
return Some(filepath);
}
match directory.parent() {
Some(parent) => directory = parent,
None => return None,
}
}
}
fn main() -> std::io::Result<()> {
let path = env::current_dir()?;
match find_rusv_file(&path) {
Some(filepath) => println!("Rusv file was found: {:?}", filepath),
None => println!("No rusv file was found."),
};
Ok(())
}
My question is not about:
- Creating a more generic function (e.g. by giving the filename as an argument);
- Anything related to the
main()
function.