|
|
|
|
|
by 19h
1191 days ago
|
|
I took the insertion_sort impl from the bottom of the post and asked gpt4 to rewrite it into idiomatic Rust: pub fn insertion_sort(n: i32, p: &mut [i32]) {
for i in 1..n as usize {
let tmp = p[i];
let mut j = i;
while j > 0 && p[j - 1] > tmp {
p[j] = p[j - 1];
j -= 1;
}
p[j] = tmp;
}
}
fn main() {
let mut arr1: [i32; 3] = [1, 3, 2];
insertion_sort(3, &mut arr1);
// …
}
I guess if this actually works, we can translate massive amounts of internal C libraries into human readable Rust... good stuff.(funnily enough, passing in the "original" code without the `unsafe extern "C"` part makes it produce the exact same output as the above) |
|
Here, who says the idiomatic translation is not .sort()? It should use the stdlib.