Logo
Articles Compilers Libraries Books MiniBooklets Assembly C++ Rust Go Linux CPU Others Videos
Advertisement

Article by Ayman Alheraki on January 11 2026 10:32 AM

Hardware Access with Rust Retrieving Hard Drive Information (Windows)

Hardware Access with Rust: Retrieving Hard Drive Information (Windows)

Rust example using the system_diskinfo crate to retrieve hard drive data, including the physical serial number:

Explanation:

  1. Import: Import the necessary functions from the system_diskinfo crate.

  2. Retrieve Information:

    • Call drive_serial_number(), drive_model(), drive_size(), and drive_name() to fetch the respective information.

    • These functions return a Result type, which can be either Ok (containing the data) or Err (indicating an error).

  3. Handle Results:

    • Use

      to handle both success and error cases:

      • If the result is Ok, print the retrieved information.

      • If the result is Err, print an error message.

  4. Drive Size Conversion: Convert the drive size from bytes to gigabytes (GB) for better readability.

Key Points:

  • system_diskinfo Crate: This crate simplifies the process of accessing disk information on Windows systems. It abstracts away the low-level WinAPI calls, making it easier to use in Rust.

  • Error Handling: Rust's Result type and the match expression ensure robust error handling, making your code more reliable.

  • Platform Specificity: Note that the system_diskinfo crate is currently designed for Windows. If you need to get disk information on other platforms (like Linux or macOS), you'll need to use different libraries or system calls.

To use this example:

  1. Add Dependency: Add the system_diskinfo crate to your project's Cargo.toml file:

     

  2. Run: Compile and run the Rust code. It should print the serial number, model, size, and name of your system's hard drive.

Advertisements

Responsive Counter
General Counter
1279159
Daily Counter
4399