18 releases

0.6.4 Apr 6, 2023
0.6.2 Dec 28, 2022
0.6.1 Feb 21, 2022
0.5.1 Aug 23, 2021
0.2.4 Feb 3, 2019

#38 in Text processing

Download history 12744/week @ 2023-12-12 13610/week @ 2023-12-19 8196/week @ 2023-12-26 17328/week @ 2024-01-02 20125/week @ 2024-01-09 22335/week @ 2024-01-16 22969/week @ 2024-01-23 20050/week @ 2024-01-30 22909/week @ 2024-02-06 24641/week @ 2024-02-13 26203/week @ 2024-02-20 26152/week @ 2024-02-27 30068/week @ 2024-03-05 27938/week @ 2024-03-12 28336/week @ 2024-03-19 23403/week @ 2024-03-26

114,175 downloads per month
Used in 146 crates (16 directly)

MIT license

69KB
1K SLoC

prettydiff

Crate docs.rs

Side-by-side diff for two files in Rust. App && Library.

Examples

Slice diff

use prettydiff::diff_slice;

println!("Diff: {}", diff_slice(&[1, 2, 3, 4, 5, 6], &[2, 3, 5, 7]));
println!(
    "Diff: {}",
    diff_slice(&["q", "a", "b", "x", "c", "d"], &["a", "b", "y", "c", "d", "f"])
);
println!(
    "Diff: {}",
    diff_slice(&["a", "c", "d", "b"], &["a", "e", "b"])
);

diff_slice

Get vector of changes:

use prettydiff::diff_slice;

assert_eq!(
    diff_slice(&["q", "a", "b", "x", "c", "d"], &["a", "b", "y", "c", "d", "f"]).diff,
    vec![
        DiffOp::Remove(&["q"]),
        DiffOp::Equal(&["a", "b"]),
        DiffOp::Replace(&["x"], &["y"]),
        DiffOp::Equal(&["c", "d"]),
        DiffOp::Insert(&["f"]),
    ]
);

Diff line by chars or words

diff_chars

use prettydiff::{diff_chars, diff_words};

println!("diff_chars: {}", diff_chars("abefcd", "zadqwc"));
println!(
    "diff_chars: {}",
    diff_chars(
        "The quick brown fox jumps over the lazy dog",
        "The quick brown dog leaps over the lazy cat"
    )
);
println!(
    "diff_chars: {}",
    diff_chars(
        "The red brown fox jumped over the rolling log",
        "The brown spotted fox leaped over the rolling log"
    )
);
println!(
    "diff_chars: {}",
    diff_chars(
        "The red    brown fox jumped over the rolling log",
        "The brown spotted fox leaped over the rolling log"
    )
    .set_highlight_whitespace(true)
);
println!(
    "diff_words: {}",
    diff_words(
        "The red brown fox jumped over the rolling log",
        "The brown spotted fox leaped over the rolling log"
    )
);
println!(
    "diff_words: {}",
    diff_words(
        "The quick brown fox jumps over the lazy dog",
        "The quick, brown dog leaps over the lazy cat"
    )
);

Diff lines

diff_lines

use prettydiff::diff_lines;

let code1_a = r#"
void func1() {
    x += 1
}

void func2() {
    x += 2
}
    "#;
let code1_b = r#"
void func1(a: u32) {
    x += 1
}

void functhreehalves() {
    x += 1.5
}

void func2() {
    x += 2
}

void func3(){}
"#;
println!("diff_lines:");
println!("{}", diff_lines(code1_a, code1_b));

App

This crate also provides app for side-by-side diff:

cargo install prettydiff
prettydiff left_file.txt right_file.txt

App

Dependencies

~0.1–9.5MB
~59K SLoC