walkfile

遍历指定目录下的所有文件夹,文件

1 unstable release

0.1.0 Jun 8, 2024

MIT license

7KB
76 lines

walkfile

Struct walkfile::WalkFileEntry

pub struct WalkFileEntry {
    pub root: PathBuf,
    pub child_dirs: Vec<String>,
    pub child_files: Vec<String>,
}

WalfFileENtry

  • root: 根目录
  • child_dirs: root下所有的子文件夹名称
  • child_files: root下所有的文件

Function walkfile::walkCopy

pub fn walk(path: &PathBuf) -> Result<Vec<WalkFileEntry>>

遍历指定根目录的入口函数

use std::{path::PathBuf, str::FromStr};
use crate::walk;
use anyhow::{Ok, Result}; 
fn test_walk(){
let res = walk(&PathBuf::from_str("./").unwrap());
let res = match res {
    Err(e) => panic!("{}", e),
    Ok(res) => res
};
print!("{}", res[1]);
} 

引入

cargo.toml

[dependencies]
walkfile = "*"

使用

fn test_ref_tuple(){
    let res = walk(&PathBuf::from_str("./").unwrap());
    let res = match res {
        Err(e) => panic!("{}", e),
        Ok(res) => res
    };
    let (root, child_dirs, child_files) = res[0].as_tuple();
    println!("{:?}, {:?}, {:?}", root, child_dirs, child_files);
}

输出

[
    WalkFileEntry {
        root: "./src",
        child_dirs: [],
        child_files: [
            "lib.rs",
        ],
    },
    WalkFileEntry {
        root: "./",
        child_dirs: [
            "src",
        ],
        child_files: [
            "Cargo.toml",
            "readme.md",
        ],
    },
]

Dependencies

~135KB