19 releases (stable)

2.6.0 Jul 21, 2024
2.5.0 Mar 28, 2024
2.4.0 Dec 29, 2023
2.3.0 Oct 7, 2023
0.5.0 Feb 1, 2021

#3 in Text processing

Download history 432137/week @ 2024-07-18 441801/week @ 2024-07-25 457574/week @ 2024-08-01 492057/week @ 2024-08-08 478463/week @ 2024-08-15 481605/week @ 2024-08-22 440144/week @ 2024-08-29 504984/week @ 2024-09-05 480399/week @ 2024-09-12 477537/week @ 2024-09-19 504935/week @ 2024-09-26 540812/week @ 2024-10-03 518481/week @ 2024-10-10 556076/week @ 2024-10-17 570910/week @ 2024-10-24 537058/week @ 2024-10-31

2,303,512 downloads per month
Used in 1,854 crates (150 directly)

Apache-2.0

185KB
4K SLoC

Similar: A Diffing Library

Crates.io License rustc 1.60.0 Documentation

Similar is a dependency free crate for Rust that implements different diffing algorithms and high level interfaces for it. It is based on the pijul implementation of the Patience algorithm and inherits some ideas from there. It also incorporates the Myer's diff algorithm which was largely written by Brandon Williams. This library was built for the insta snapshot testing library.

use similar::{ChangeTag, TextDiff};

fn main() {
    let diff = TextDiff::from_lines(
        "Hello World\nThis is the second line.\nThis is the third.",
        "Hallo Welt\nThis is the second line.\nThis is life.\nMoar and more",
    );

    for change in diff.iter_all_changes() {
        let sign = match change.tag() {
            ChangeTag::Delete => "-",
            ChangeTag::Insert => "+",
            ChangeTag::Equal => " ",
        };
        print!("{}{}", sign, change);
    }
}

Screenshot

terminal highlighting

What's in the box?

  • Myer's diff
  • Patience diff
  • Hunt–McIlroy / Hunt–Szymanski LCS diff
  • Diffing on arbitrary comparable sequences
  • Line, word, character and grapheme level diffing
  • Text and Byte diffing
  • Unified diff generation

Dependencies

~0–650KB