gpt4 book ai didi

memory - 为什么在Rust中 “capture by reference”与 “capture a reference by value”等效?

转载 作者:行者123 更新时间:2023-12-03 11:27:33 26 4
gpt4 key购买 nike

摘自Huon Wilson的Finding Closure in Rust:

Capturing entirely by value is also strictly more general than capturing by reference: the reference types are first-class in Rust, so "capture by reference" is the same as "capture a reference by value". Thus, unlike C++, there’s little fundamental distinction between capture by reference and by value, and the analysis Rust does is not actually necessary: it just makes programmers’ lives easier.


我竭尽全力解决这个问题。如果捕获“按值”引用,是否捕获了存储在堆中的数据?还是引用了在堆栈上找到的引用的指针值?

最佳答案

Or does it refer to the pointer value of the reference, which is found on the stack?


是的,ish。
在Rust中,引用是经过规范化的,它们是您实际操作的东西。因此,当您按值捕获引用时,您捕获的是引用本身(指针,这实际上是Rust引用),而不是引用者(指针)。
按引用捕获基本上只是隐式创建一个引用并按值捕获它。

关于memory - 为什么在Rust中 “capture by reference”与 “capture a reference by value”等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65180209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com