Access is denied. (os error 5)

I got some permission issue when I try to print the Option variable like below


it always show me the Access is denied. (os error 5)

but when remove the Option variable, print the message directly, the cargo run will work perfectly

fn main() {
    println!("Hello");
}

does anyone know how to fix it?

Version: rustc 1.77.1 (7cf61ebde 2024-03-27)

There is absolutely no way I can imagine that Option is causing that error. That'd be like turning on the "Hide Taskbar" setting causing your GPU to catch fire.

My first suspect would be a virus scanner behaving badly. Failing that, perhaps the editor you're using is doing something weird (try compiling and running the code without the IDE running at all). Next would be something wrong with your Rust install (try removing and reinstalling it from scratch). Next would be very strangely configured security permissions on that folder, or one of its parents.

If it's not any of those, consider an exorcist because your machine might be haunted.

12 Likes

I agree. Sounds like a virus scanner preventing you from running the executable.

4 Likes

Thank you for your reply, I don't know why, and have tried not using the IDE, but I still get the same error.

It's really weird. It looks like related with println!.

fn main() {
    println!("No max value configured.")
}

If I try to print the message "No max value configured." it says "Access denied" (OS error 5)., it looks like it has a limit of 23 characters

I think maybe you can move your project to other location(like D:\ ) and try to execute it again.

Have you tried adding project folder to your antivirus's exclude list?

Certainly smells like getting caught by antivirus which ignores files under some size threshold presumed to not be big enough to cause an issue

2 Likes

I did it, and it still show the "Access denied" :smiling_face_with_tear:

Can you try running it with --release? and are you using any dependencies?

yes I also try to --release and it still show the Access is denied., no I don't using any dependencies

hmmm...also close the antivirus but still not working :smiling_face_with_tear:

Perhaps you could run procmon and see what specific access is triggering the "Access denied"?

I can't help shake the feeling that I have seen this before -- a very long time ago..

1 Like

cargo clean
...in a command shell does what? Works?

cargo run
...in a command shell does what? Fails?

Which command shell did you use?

I second this. When I run into Access Denied on Linux, the first thing I do is get an strace log and see what it was trying to access. Much less frustrating than guesswork.

  1. Cargo clean and Cargo build are worked

  2. the Cargo run still show access denied

  3. I am using the Power shell

Add your projects directory, C:\Users\hus\RustroverProjects, to the Microsoft Defender exclusions list. It's buried under Virus & Threat Protection Settings > Exclusions.

From the same command shell, after cargo run, does the target (.\target\debug\hello.exe) exist? Are you able to run the target directly?

Does using the "classic" command shell (CMD.EXE) make a difference?

Regardless of all that, placing your project in a folder under the Desktop is likely to cause problems. You at least need to move it to Documents.

Have you tried just running the exe file directly in the terminal?

emmm,try to execute your code as super user?