GCC's weak attribute


I’m looking at doing some embedded work with Rust as a test to see how it would fit for some projects. One of the things I would like to do is emulate something I do with GCC and C right now with the weak attribute. It’s defined like this (which you probably know):


The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations. Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker.

Is it possible to do something similar to this with Rust? I’ve tried searching as best as I could, but couldn’t find anything that really answered my question.

#[linkage = "weak"]
pub fn f() { }

Unstable, so only available on nightly builds. I’m a little curious why you care, though… it’s not something which normally gets used much, even in C.


I use it in my embedded C code in my main application core so that people can write their own versions of certain functions if needed, but will drop back to the default function implementation if not.