Porting Code

TODO This section will provide a more real world C/C++ example and port it to the equivalent Rust

Corrode is a command-line tool that can partially convert C into Rust. At the very least it may spare you some drudgery ensuring the functionality is as close to the original as possible.

Corrode will take a C file, e.g. plus any arguments from gcc and produces a which is the equivalent code in Rust.

It works by parsing the C code into an abstract syntax tree and then generating Rust from that.

Interestingly Corrode is written in Haskell and more interestingly is written as a - the code is a markdown document interspersed with Haskell.

Bindgen

Bindgen is a tool for generating FFI interfaces for Rust from existing C and C++ header files. You might find this beneficial if you’re porting code from C / C++, or writing a new component that must work with an existing code base.

The readme documentation on the site link provides more information on installing and using the tool.

Experiences

A number of websites offer insights of the porting process from C to Rust

  1. TODO

Tips

Use references wherever you can

TODO references are equivalent to C++ references or to pointers in C. Passing by reference is an efficient way of passing any object greater than 64-bits in size into a function without relinquishing ownership. In some cases, it is even a good idea to return by reference since the caller can always clone the object if they need to, and more often than not they can just use the reference providing the lifetimes allow for it.

TODO you do not need to use references on intrinstic types such as integers, bools etc. and there is no point unless you intend for them to be mutable and change.

C and C++ default to copy on assign, Rust moves on assign unless the type implements the Copy trait. This is easily one of the most mind boggling things that new Rust programmers will encounter. Code that works perfectly well in C++ will instantly fail in Rust.

The way to overcome this is first use references and secondly don’t move unless you intend for the recipient to be the new owner of an object.

Use modules to naturally arrange your source code

TODO

Using composition and traits

TODO Rust does not allow you to inherit one struct from another. The manner of overcoming this.

Use Rust naming conventions and formatting

C and C++ have never had

Foreign Function Interface

TODO for now read the .

Leaving function names unmangled

TODO attribute no_mangle

TODO Rust provides a crate with bindings for C library functions. If you find yourself receiving a pointer allocated with malloc you could free it with the corresponding call to free() via the bindings.

TODO example of using libc