Skip to main content

diff_text

Function diff_text 

Source
pub fn diff_text(text1: &str, text2: &str) -> Result<String>
Expand description

Generates a colored, side-by-side diff of two text strings.

This function compares two text strings line by line and produces a formatted diff output with color coding and line numbers. The output uses:

  • Red color for deleted lines (-)
  • Green color for added lines (+)
  • Dim style for unchanged lines
  • Underlined text for emphasized changes within lines

§Arguments

  • text1 - The original text (left side of the diff)
  • text2 - The modified text (right side of the diff)

§Returns

A Result<String> containing the formatted diff output, or an error if the diff generation fails.

§Examples

use xdiff_live::diff_text;

let text1 = "hello\nworld";
let text2 = "hello\nrust";
let diff = diff_text(text1, text2).unwrap();
println!("{}", diff);

§Output Format

The output format shows:

  • Line numbers for both old and new text
  • A separator character (|)
  • The diff marker (-, +, or space)
  • The actual line content with highlighting

Groups of changes are separated by horizontal lines for better readability.