内联Rust中的常量值意味着什么? [英] What does it mean for a constant value in Rust to be inlined?

查看:107
本文介绍了内联Rust中的常量值意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<$的文档c $ c> const


常量在程序的整个生命周期中都有效。更具体地说,Rust中的常量在内存中没有固定地址。这是因为它们可以有效地内联到使用的每个位置。出于这个原因,不一定要引用相同的常量。

Constants live for the entire lifetime of a program. More specifically, constants in Rust have no fixed address in memory. This is because they’re effectively inlined to each place that they’re used. References to the same constant are not necessarily guaranteed to refer to the same memory address for this reason.

我只看到内联函数,但绝不内联常量值。初学者对此的解释是什么?

I've only seen "inline functions" in C++, but never inline constant values. What is a beginner friendly explanation of how this works?

内存中没有固定地址也让我感到困惑。这是否意味着每次我们使用 const 值时,都会为该表达式分配 stack 上的值,并且在表达式执行完毕后,它会被摧毁

I'm also confused by "no fixed address in memory". Does that mean every time we use a const value, a value on the stack is allocated just for this expression and after the expression is done executing, it'll be destroyed?

推荐答案


我只看到C ++中的内联函数,但绝不内联常量值。

I've only seen "inline functions" in C++, but never inline constant values.

在Rust中最接近 const 的近似值是枚举在C ++中。

The closest approximate to a const in Rust is an enum in C++.


这对初学者的解释是什么?

What is a beginner friendly explanation of how this works?

一个简单的初学者的解释是:它是可行的,不用担心具体细节。

The simple beginner's explanation is: it just works, don't worry about the nitty-gritty details.


我也对内存中没有固定地址感到困惑。这是否意味着每次我们使用 const 值时,都会为该表达式分配 stack 上的值,并且在表达式执行完毕后,它会被破坏

I'm also confused by "no fixed address in memory". Does that mean every time we use a const value, a value on the stack is allocated just for this expression and after the expression is done executing, it'll be destroyed?

是。也许。否。

这恰好是它在罐子上说的:不作保证

It means exactly what it says on the tin: no guarantee is made. This leaves the compiler with the maximum freedom to optimize things.

好的,这一切都很好,但是...真正发生了什么?

Alright, that's all good and well, but... what really happens?

在实践中,有两种情况:

In practice, there are two situations:


  • 值很简单:它甚至不接触堆栈,而是直接在程序集中进行硬编码。例如,这最有可能发生在整数上。

  • 值并不是那么简单:它在只读存储器中创建,然后从那里进行引用/复制。堆栈上的多个副本将具有不同的地址。

简单是什么意思?这要看情况。对于每个调用站点,编译器可能会决定是否足够简单,这接近内联。

What does simple mean? Well, it depends. For each call site the compiler may decide "simple enough" or not, which is where it is close to inlining.


这意味着每个当我们使用 const 值时, stack 上的一个值就是为此表达式分配的,并且在表达式执行完后,它将是被摧毁

Does that mean every time we use a const value, a value on the stack is allocated just for this expression and after the expression is done executing, it'll be destroyed?

它不会被销毁。 const 变量不能具有实现 Drop 的类型。当不再使用时,该值只是忘记了。如果它曾经占用堆栈中的内存,则该内存将在以后的某个时间被覆盖。

It will not be destroyed. const variables cannot have a type that implements Drop. The value is just forgotten when it is no longer used. If it ever occupied memory on the stack, this memory will be overwritten sometime later.

这篇关于内联Rust中的常量值意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆