DEV Community

Fiza Sheikh
Fiza Sheikh

Posted on

Port Mortem 2026 · ported pylev Python Rust · 250k diff tests, 0 mismatches · the Unicode indexing trap is real

Port Mortem Write-Up: Python pylev → Rust

Here's what actually happened when I picked the smallest repo on the list.

Hey, I'm Fiza. Repo: https://github.com/Fiza26u12/pylev-rs


What I picked and why

Discord message was clear: "the small library you can prove beats the ambitious one you can't."

Everyone else probably grabbed something that sounds impressive. I grabbed "pylev" — 300 lines, Levenshtein distance, five functions, has tests. Boring on paper. That's why I picked it.

If I can't prove my port matches Python on 250,000 inputs, I don't want to be in the hackathon.


The thing that would've failed silently

This is the part I'd have been embarrassed about if a judge found it before I did.

Python: len("café") → "4"
Rust: "café".len() → "5" (bytes, not characters)

Every ASCII test in the original suite passes with wrong indexing. You feel done. You're not.

I had to convert to Vec<char> in every function. Slower. Correct.

My differential tests include café vs cafe\u0301 (precomposed vs combining accent), CJK, emoji. All pass now. None would pass with naive byte indexing.

This ate the most thinking time — not the DP algorithm, the string model.


What I actually built

Six functions ported: levenshtein, classic, recursive, wf, wfi, damerau. Pure Rust. Zero unsafe. No PyO3 wrapper (that would've been disqualification anyway).

Hardest non-algorithm bug: "damerau row rotation". Python does dprev, d0, d1 = d0, d1, dprev. Get the order wrong in Rust and foobar/foobra returns wrong distance. No crash. Just a wrong number. Original test caught it.


** How I proved it (the part that matters)**

Layer 1: Python's 22 tests pass. Rust parity tests pass.

Layer 2: Python script generates 20,000 inputs, saves expected answers to a file in the repo. Rust replays them in cargo test. "Judge doesn't need Python installed"

Layer 3: Live run — 250,000 cases, Python vs Rust, "0 mismatches". Saved in artifacts/differential_report.json.

Did I find bugs in the original? No. I wanted to — there's a prize for that. The library is 10+ years old and correct. I'd rather say that honestly than invent a bug.


What broke (not code — my laptop)

  • No MSVC linker → switched to GNU Rust toolchain
  • Windows Defender "deleted my compiled binary" mid-hackathon. Renamed it, moved build folder out of OneDrive, still flagged. Had to stop relying on CLI and use cargo test only. Git push failed because Credential Manager had wrong GitHub account (fiza-beep vs Fiza26u12)

The port worked. The environment fought me.


Benchmarks (honest)

~47-83x faster on iterative algorithms on my laptop. Recursive only ~11x because both sides use hash maps.

I wanted to use Criterion. Couldn't — GNU toolchain on Windows needed dlltool.exe I didn't have. Hand-rolled timing instead. Numbers are directionally right, not lab-grade.

Both versions allocate per call. A faster Rust version would need a different API. I chose correctness.


What I'd take back

Generate committed test vectors on day one. I spent time on a CLI diff harness that Defender eventually killed. The vectors in the repo are what actually prove the port. Everything else is optional.

Also: read about Unicode before writing line one of Rust code. Would've saved hours of second-guessing.



## Verify it yourself

Enter fullscreen mode Exit fullscreen mode


bash
git clone https://github.com/Fiza26u12/pylev-rs
cd pylev-rs
cargo test




20,000 Python-generated checks, no Python needed.

Original: https://github.com/toastdriven/pylev

---

**Port Mortem 2026 · Hackathon Raptors**
Enter fullscreen mode Exit fullscreen mode

Top comments (0)