Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Return vector of Path or PathBuf

  • Path

  • PathBuf

  • We would like to create a function that collectes Path objects and returns them.

  • Instead of Path objects we'll return PathBuf objects

use std::path::{Path, PathBuf};

fn main() {
    let pathes = get_pathes();
    println!("{:?}", pathes);
    for path in pathes {
        println!("{:?}", path);
        //println!("{}", path.display());
        //println!("{:?}", path.parent());
    }
}

fn get_pathes() -> Vec<PathBuf> {
    let mut entries = vec![];
    let path = Path::new(".");
    for entry in path.read_dir().expect("read_dir call failed").flatten() {
        entries.push(entry.path());
    }
    entries
}