Loading tutorials
Result. You should be able to create and build a Cargo project. You should also know what a C pointer is, and what unsafe means in Rust. You don't need to have written any FFI code yet.4 min readResult), can run a Cargo project, and know what unsafe means. It also assumes you can read a C prototype. You need Rust 1.82 or newer for the unsafe extern syntax used here.2 min readextern block and call a C function from unsafe Rust, as in the previous chapter. It also assumes you know what a raw pointer is. Here you will learn how to choose the Rust type that matches each C type in a declaration.3 min readunsafe extern "C" { fn strlen(...) }, and know what *const c_char means. The examples need Rust 1.82 or newer for unsafe extern blocks.3 min readunsafe Rust, and that you know the C types in std::ffi. It builds on the earlier chapters on extern blocks and C-compatible types.3 min readextern block and call a C function from unsafe code, and that you know the basics of Cargo. Declaring a function tells the compiler its signature. Linking is the step that finds its machine code. Without it, a correct declaration still fails to build.3 min readextern block, and have read the earlier chapters on #[repr(C)] structs and linking. You will compile a small C file as part of cargo build and call it from Rust.2 min readDrop, Option), know C pointers and malloc/free, and have already declared and called C functions in unsafe blocks. It covers the part of FFI that causes the most crashes: deciding who frees each piece of memory.2 min readextern functions, use #[repr(C)] structs, and reason about who owns a C allocation. It shows how to hide the unsafe calls behind types that callers cannot misuse.2 min readextern functions, call them in unsafe blocks, and use CStr for strings. It also assumes you know Rust's Result and ? operator. Here you will convert C's error conventions into idiomatic Rust errors, so callers of your safe wrapper never see raw status c3 min readunsafe extern "C" block, use raw pointers, and write #[repr(C)] structs. It shows how to hand C a Rust function to call back, and how to give that function access to Rust state.3 min readextern "C" functions use the C ABI. It covers why a panic must not cross into C and how to stop it.3 min readextern blocks, use #[repr(C)] structs, and wrap raw calls in a safe API. It also assumes you have a Cargo project that compiles C with build.rs. Here you will let a tool write the declarations you have been typing by hand.3 min read