Rust Usage with Cargo
Prerequisites
Before proceeding, ensure the following are installed and set up:
- Rust Toolchain: Install Rust using rustup.
- filerix: Ensure the
filerixlibrary and its development files are installed.
Installing Dependencies
Depending on your distribution, install Rust and the required build tools:
sh
sudo dnf install cargo rustcsh
sudo apt install cargo rustcsh
sudo pacman -Syu cargo rustCreating a Rust Project
Create a new Rust project:
bashcargo new example cd exampleAdd
filerixto yourCargo.toml:toml[dependencies] filerix = "0.1"
Building the Example
- Compile the project with Cargo:bashThis will generate the executable in
cargo build --releasetarget/release/example.
Running the Example
- Run the compiled binary:bash
./target/release/example - The example will fetch and log the disk usage information for the specified path (
/by default). You should see output like this:bashUsed space: <used_bytes> bytes Total space: <total_bytes> bytes
Understanding the Code
The Rust code interacts with the filerix library to fetch the disk usage of a given directory (/ by default). Here’s a quick breakdown of the key components:
get_drive_usage(): Retrieves the disk usage details (used space and total space) for the given path.println!: Prints the result to the console.
This example demonstrates how to use filerix for filesystem operations and integrate it into a Rust project with Cargo.
Common issues
- If you encounter issues with library loading (e.g.,
filerix.sonot found), ensure the library path is correctly set:bashexport LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH
