languages icon indicating copy to clipboard operation
languages copied to clipboard

Changed Rust code to use more idiomatic iterator pattern.

Open Ichoran opened this issue 1 year ago • 0 comments

The previous code used indexing from ranges e.g. for i in 1..=m. While this works, it's usually slower and more error-prone than iterator-based traversal. This patch switches to the more idiomatic .iter().enumerate() pattern since this is how you would usually do this in Rust.

This does seem also to improve performance (~20% on my machine).

Furthermore, the working space for the two rows was initialized in an atypical way: instead of creating the working space with the content and size desired, it created empty Vecs and then put content into it. The Vecs are now initialized with the correct content directly.

This did not alter performance on my machine, but it makes the code easier to read (because the pattern is more expected).

I also switched the argument reading to iter().enumerate() since it also is more expected. (No performance implication.)

None of this changes the algorithm from what is used in the C reference; rather, it expresses the same algorithm in a more idiomatic Rust style.

Ichoran avatar Dec 22 '24 01:12 Ichoran