In the following example, I believe that when the function Struct::new is called, its body will be inlined (copied) to the call-site because of the #[inline] attribute. This will result in more code being generated and slower compile times but better runtime performance because of the eliminated function call.
impl Struct {
#[inline]
pub fn new() -> Self {
// initialization code
}
}
Nowadays const fn are stable on Rust. When someone declares a function as const, it should allow the compiler to evaluate it at compile time.
impl Struct {
pub const fn new() -> Self {
// initialization code
}
}
Does declaring the function as const and omitting the #[inline] attribute offer the same trade-offs as the code in the previous example (because the function is evaluated in compile-time and its result is "inlined" to the call-site)? Should we replace all the #[inline] attributes with const? If they are not same, what are the differences?