chapter three

3 Introduction to C FFI and unsafe Rust

 

This chapter covers

  • Performing normally forbidden operations with unsafe Rust
  • Refactoring a component of a C program into Rust

The last chapter provided a high-level overview of Rust code, and discussed some elements of Rust that may be surprising or difficult to understand for new developers. Now that we’re able to write simple Rust programs, this chapter will walk through an example of how to embed Rust code within an existing C program.

3 Introduction to C FFI and unsafe Rust

3.1 Unsafe Rust

3.1.1 Raw Pointers

3.2 C FFI

3.2.1 Including a crate

3.2.2 Creating a dynamic library with Rust

3.2.3 Solving arithmetic expressions in Rust

3.2.4 The Display Trait

3.3 Summary