我有以下 Rust 代码:
#[inline(never)]
fn x() -> i32 {
21
}
pub fn main() -> i32 {
x()
}
Run Code Online (Sandbox Code Playgroud)
如果没有优化 ( -C opt-level=0),函数调用不会内联,即使#[inline(never)]被删除也是如此。
example::x:
mov eax, 21
ret
example::main:
push rax
call example::x
mov dword ptr [rsp + 4], eax
mov eax, dword ptr [rsp + 4]
pop rcx
ret
Run Code Online (Sandbox Code Playgroud)
但是,任何其他优化级别都会导致函数被内联,无论如何。
example::main:
mov eax, 21
ret
Run Code Online (Sandbox Code Playgroud)
我知道该inline属性只是对编译器的建议,但我似乎找不到任何示例,其中 using#[inline(never)]实际上会阻止函数被内联,否则它会被内联。
这是编译器资源管理器结果:https ://godbolt.org/z/WKfevdW37
rust ×1