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
  1. 1File or directory is not readable/writable by the current user
  2. 2SELinux/AppArmor policy restriction
  3. 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")?; // PermissionDenied
Sources

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Rust errors