How to reproduce C-style external function calls in Rust?

⚓ Rust    📅 2026-02-20    👤 surdeus    👁️ 6      

surdeus

Hello, I'm a junior dev moving from C to Rust. I'm currently trying to port a library from C to Rust and I'm struggling to reproduce a specific behavior.

In C, I had the following:

// my_lib.h
typedef struct {
    uint8_t i2c_address;
} my_struct_t;

uint8_t send_to_i2c(my_struct_t *s);

// my_lib/some_file.c
void some_func(my_struct_t *s) {
    send_to_i2c(s);
}

In this setup, the library user had to implement the send_to_i2c function. I can't figure out how to achieve this in Rust. How can my library know that this function exists in a safe way while leaving the implementation to the client? Are the Trait the solution?

Thanks for you time!
Ben

3 posts - 3 participants

Read full topic

🏷️ Rust_feed