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

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

问题描述

文档 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.

我只见过 C++ 中的内联函数",但从未见过内联常量值.什么是对它如何工作的初学者友好的解释?

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++ 中的 enum.

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天全站免登陆