#define in rust

in C ,I have #define mult(x,y) x*y; to make the code clearly but still help program performance maintainable. how can I do this in rust

But also, the only reason your defile-less version has extra instructions, is because you do not pass opt level flag to the compiler, which you would never not do in reality.

6 Likes

If you care about performance, you should pass -O3 to C/C++ or -C opt-level=3 to rustc (or cargo build --release if you use cargo). That should generally result in the same performance for macros and (inline) functions in cases like yours.

4 Likes

thank you

Note, there's a separate forum for asking how to do something in Rust, https://users.rust-lang.org/

This forum is for discussing new changes to the language mostly

5 Likes

understood , thank you

Keep in mind that in Rust, and in modern C, you don't need to use macros for performance. Most of the time, you can use an inline function instead: static inline in C, #[inline] in Rust.

12 Likes

As long as the function is used in the same crate, or you're using LTO (and if you care about performance you should) then you don't need to make it #[inline] either.

5 Likes