thediff

Crates.iothediff
lib.rsthediff
version0.1.1
created_at2025-12-16 07:46:33.402497+00
updated_at2025-12-16 08:52:58.406029+00
descriptionDifference between 2 files in percentages
homepage
repository
max_upload_size
id1987361
size4,875
Hayk Atshemyan (Atshemyan)

documentation

README

thediff

A small Rust crate for calculating how different two files are, expressed as a percentage.
It is useful for file comparison, integrity checks, similarity analysis, or detecting changes between binary or text files.


Installation

Add the crate to your Cargo.toml:

[dependencies]
thediff = "0.1"

Usage

Compare Two Strings


use thediff::thediff_strings;

fn main() -> std::io::Result<()> {
    let (diff_percent, same_percent) = thediff_strings("0101010", "01010111")?;
    println!(
        "Different part: {:.2}%, Same part: {:.2}%",
        diff_percent, same_percent
    );
    Ok(())
}

Compare Two Files

use std::path::Path;

use thediff::thediff_files;

fn main() -> std::io::Result<()> {
    let (diff_percent, same_percent) = thediff_files(Path::new("file1.bin"), Path::new("file2.bin"))?;
    println!(
        "Different part: {:.2}%, Same part: {:.2}%",
        diff_percent, same_percent
    );
    Ok(())
}

use std::path::Path;
use thediff::thediff_files_async;

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let (diff_percent, same_percent) =
        thediff_files_async(
            Path::new("file1.bin"),
            Path::new("file2.bin"),
        )
        .await?;

    println!(
        "Different part: {:.2}%, Same part: {:.2}%",
        diff_percent, same_percent
    );

    Ok(())
}


How It Works

The crate compares files utf8 char by char:

  • Counts how many chars differ

  • Handles files of different sizes

  • Extra bytes in the longer file are treated as differences

Examples

File A File B Result
Identical Identical 0%
50% bytes differ Same length 50%
One file empty Other non-empty 100%

Commit count: 0

cargo fmt