PermissionDenied, how can i access folder that i create

thread 'main' panicked at src\
called `Result::unwrap()` on an `Err` value: Os { code: 5, kind: PermissionDenied, message: "access denied" }

fn main() {
    copy_dir_to(Path::new("./haha"), Path::new("./lala")).unwrap();

fn copy_dir_to(src: &Path, dst: &Path) -> Result<(), std::io::Error> {
    if !src.is_dir() { fs::create_dir(dst)? }
    for entry_result in src.read_dir()? {
        let entry = entry_result?;
        let file_type = entry.file_type()?;
        copy_to(src, &file_type, &dst.join(entry.file_name()))?

fn copy_to(src: &Path, file_type: &FileType, dst: &Path) -> Result<(), std::io::Error> {
    if file_type.is_file() {
        fs::copy(src, dst)?;
        return Ok(());
    if file_type.is_dir() {
        copy_dir_to(src, dst)?;
        return Ok(());
    return Err(io::Error::new(io::ErrorKind::Other, format!("DO NOT KNOW HOW TO COPY: {}", src.display())));

You are creating the destination dir when the source dir doesn't exist. That doesn't seem right.

but i copyed them from book Programming Rust 2

The code from examples/copy/src/ at master · ProgrammingRust/examples · GitHub uses if !dst.is_dir(), not src, though.

I wasn't able to get a permission denied error, but I did get your code to crash on Windows with a stack overflow, or return a "too many open files" error on Linux. Compared to the book's code on Github, this seems to be because your code does copy_to(src, ...), but the example code does copy_to(&entry.path, ....

Can you double-check that your code is equivalent to the code from the book?


oh, I see, Sorry for my careless, thanks !!

1 Like