Loading tutorials
Result), can run a Cargo project, understand C pointers, and know that unsafe marks code the compiler cannot check. The previous chapter covered why the C7 min readextern declaration. It assumes you know basic Rust (ownership, borrowing, Result), can read a Cargo project, understand C pointers and malloc/free, and know what unsafe means. Earlier chapters showed how to de8 min readextern "C" function inside an unsafe block. It also assumes you know the difference between String, &str and raw pointers, and that you have a recent stable toolc5 min readunsafe block. It covers how to make a Rust struct have the same memory layout as a C struct, so you can pass it to C and receive it back.6 min readextern block tells the Rust compiler that a function exists. It does not tell the linker where the function lives. This chapter covers how a Rust program finds the machine code for the C functions it declares, and what goes wrong when it can't.6 min readabs and strlen from the C standard library. This chapter covers the other common case, where the C source lives in your own repository. Cargo does not compile C by default, so you add a build script that does it. Th6 min readDrop, can declare extern functions and call them in unsafe blocks, and know the C basics of malloc, free, and pointers. Here you decide who owns each piece of memory that crosses the boundary, and you encode that decision in Rust types.6 min readextern functions, call them in unsafe blocks, and use #[repr(C)], CString and CStr. It also assumes you know what a raw pointer is and how Drop works. Here you will turn raw declarations into a Rust API that callers cannot misuse.5 min readResult code, know what unsafe means, and have read the earlier chapters on extern blocks, CStr, and safe wrappers. The goal is to hide C's error conventions behind a Rust API. A caller of your wrapper should never inspect an integer to learn whether a ca5 min readextern "C" declarations, callbacks, and status codes. You should also know what a panic is in Rust (panic!, unwrap on None, integer overflow in debug builds, out-of-bounds indexing) and that the default behavior is to unwind the stac7 min readextern blocks by hand, use #[repr(C)], and write a build.rs that compiles C with the cc crate. It also assumes you know how to wrap raw calls in a safe Rust type. Here you will let a tool write the declarations instead, and then check them.6 min read