News for hackers. Stuff that matters.
Re:C2Rust: translate C into Rust code by masklinn on Saturday June 23, 2018 @02:01PM (#17382457)
It's unsafe code manipulating raw pointers, which is "intimidating and verbose" by design.

It's also explicitly typing everything, which increases verbosity.

The "clean" rust conversion would look something like this:

    fn insertion_sort<T: Copy + Ord>(p: &mut [T]) {
        for i in 1..p.len() {
            let x = p[i];

            let mut j = i;
            while j > 0 && p[j-1] > x {
                p[j] = p[j-1];
                j -= 1;
            }
            p[j] = x;
        }
    }
Although unless it's really necessary I would expect the simpler version:

    fn insertion_sort<T: Ord>(p: &mut [T]) {
        for i in 0..p.len() {
            for j in (0..i).rev() {
                if p[j] < p[j + 1] {
                    break
                }
                p.swap(j, j + 1);
            }
        }
    }

There are no comments on this comment yet.