关于内联的不确定性

内联失败时触发自引用错误的问题,在看下面这个视频时了解到的相关信息。
Rust 异步编程 async await 原理,自引用举例踩坑说明_哔哩哔哩_bilibili
总结函数调用时数据在栈上,在值内部引用了自己,在返回函数后值被弹出栈,引用的数据也就不存在了导致panic。
为什么扯到内联,是因为之前一直以为加上 inline 属性后就会内联,实际是不确定的。
内联解释:

Inlining  内联

Entry to and exit from hot, uninlined functions often accounts for a non-trivial fraction of execution time. Inlining these functions can provide small but easy speed wins.
频繁执行的未内联函数的进入和退出通常会占用相当一部分执行时间。内联这些函数可以带来一些虽小但很容易实现的速度提升。
There are four inline attributes that can be used on Rust functions.Rust
函数中可以使用四个内联属性。
  • None. The compiler will decide itself if the function should be inlined. This will depend on factors such as the optimization level, the size of the function, whether the function is generic, and if the inlining is across a crate boundary.
  • 无 。编译器将自行决定函数是否应内联。这取决于多种因素,例如优化级别、函数的大小、         函数是否为泛型以及内联是否跨越 crate 边界。
  • #[inline]. This suggests that the function should be inlined.
  • #[inline] . 这表明该函数应该内联。
  • #[inline(always)]. This strongly suggests that the function should be inlined.
  • #[inline(always)] 。这强烈建议该函数应该内联。
  • #[inline(never)]. This strongly suggests that the function should not be inlined.
  • #[inline(never)] 。这强烈建议不要将函数内联。
Inline attributes do not guarantee that a function is inlined or not inlined, but in practice  
#[inline(always)] will cause inlining in all but the most exceptional cases.
内联属性不能保证函数是否内联,但实际上,除了最特殊的情况外  #[inline(always)] 都会导致内联。
Inlining is non-transitive. If a function  f calls a function  g and you want both functions to be inlined together at a callsite to  f, both functions should be marked with an inline attribute.
内联是非传递性的。如果函数  f 调用函数  g ,并且您希望两个函数在  f 的调用处一起内联,则两个函数都应该标记为 inline 属性。
文档: Inlining - The Rust Performance Book
收获知识在rust 中对于inline 并不能保证内联,只能供编译器参考,所以不要依赖内联来实现可能panic的某些目标,因为得不到保证。

你可能感兴趣的:(rust)