ErrorKind::PermissionDenied
RustERRORCommonI/O
Permission denied
Quick Answer
Check file permissions (chmod) or run with elevated privileges.
What this means
The OS refused the operation because the process lacks the required permissions on the file or socket.
Why it happens
- 1File or directory is not readable/writable by the current user
- 2SELinux/AppArmor policy restriction
- 3Attempting to bind a privileged port (<1024) without root
Fix
Handle permission error explicitly
Handle permission error explicitly
use std::io::ErrorKind;
match std::fs::write("/etc/hosts", b"data") {
Err(e) if e.kind() == ErrorKind::PermissionDenied =>
eprintln!("Need elevated permissions"),
Err(e) => eprintln!("{}", e),
Ok(_) => {},
}Why this works
Matches PermissionDenied to give a clear user-facing message.
Code examples
Triggerrust
std::fs::write("/etc/shadow", b"x")?; // PermissionDeniedSame error in other languages
Sources
Official documentation ↗
Rust std::io
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev