C2Rust工具能够将大多数C模块翻译成语义上等价的rust代码。这些模块将单独编译,以生成兼容的对象文件。支持C99标准。 在我们的工具翻译C源代码之前,使用clang对其进行分析和类型检查。
帮助您将符合C99的代码迁移到Rust。它提供:
架构图如下:
转换示例:
C 语言源码:
void insertion_sort(int const n, int * const p) { for (int i = 1; i < n; i++) { int const tmp = p[i]; int j = i; while (j > 0 && p[j-1] > tmp) { p[j] = p[j-1]; j--; } p[j] = tmp; } }
转换后的 Rust 代码:
#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_mut)] #![feature(libc)] extern crate libc; #[no_mangle] pub unsafe extern "C" fn insertion_sort(n: libc::c_int, p: *mut libc::c_int) { let mut i: libc::c_int = 1i32; while i < n { let tmp: libc::c_int = *p.offset(i as isize); let mut j: libc::c_int = i; while j > 0i32 && *p.offset((j - 1i32) as isize) > tmp { *p.offset(j as isize) = *p.offset((j - 1i32) as isize); j -= 1 } *p.offset(j as isize) = tmp; i += 1 }; }