get system total disk space in rust

To get the system total disk space in Rust, we can use the sys-info crate. This crate provides a convenient way to retrieve system information, including disk capacity.

Here is an example Rust code snippet that prints the total capacity of the system's primary disk:

main.rs
use sys_info::{DiskExt, SystemExt};

fn main() {
    let system = sys_info::system_info().unwrap();

    // Get the primary disk device name
    let primary_disk_name = system.get_disks().first().unwrap().get_name();

    // Get the total capacity of the primary disk
    let total_disk_space = system.get_total_disk_space(primary_disk_name).unwrap();

    println!("Total disk space: {} bytes", total_disk_space);
}
417 chars
14 lines

In this code, we first use sys_info::system_info() to obtain a System struct that holds various system information. We then use system.get_disks().first().unwrap().get_name() to get the name of the primary disk. Finally, we use system.get_total_disk_space(primary_disk_name).unwrap() to get the total capacity of the primary disk in bytes.

gistlibby LogSnag