- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在查看一些使用的代码
Rc<RefCell<SomeStruct>>
Rc enables multiple owners of the same data; Box and RefCell have single owners.
Box allows immutable or mutable borrows checked at compile time; Rc allows only immutable borrows checked at compile time;
RefCell allows immutable or mutable borrows checked at runtime. Because RefCell allows mutable borrows checked at runtime, you can mutate the value inside the RefCell even when the RefCell is immutable.
Rc
确保
SomeStruct
可供多人同时访问。但我如何访问?我只看到
get_mut
方法,它返回一个可变引用。但是文本解释了“Rc 只允许不可变的借用”。
最佳答案
So,
Rc
makes sure thatSomeStruct
is accessible by many people at the same time. But how do I access?
x
类型的变量
Rc<...>
,则可以使用
*x
访问内部值。在许多情况下,这是隐式发生的;例如,您可以简单地使用
x
调用
x.method(...)
上的方法。
I only see the
get_mut
method, which returns a mutable reference. But the text explained that "Rc
allows only immutable borrows".
get_mut()
方法可能比
Rc
只允许不可变借用的解释更新。此外,如果当前只有一个内部值的所有者,即如果您目前首先不需要
Rc
,它只会返回一个可变借用。一旦有多个所有者,
get_mut()
将返回
None
。
If it's possible to access
Rc
's object in mut and not mut way, why aRefCell
is needed?
RefCell
也将允许您获得可变访问权限,即使您只持有对
RefCell
的共享引用。它会在运行时动态检查在任何给定时间只存在一个可变引用,并且如果您请求第二个并发引用(或分别返回和
try_borrow
方法的错误),它会发生 panic 。
Rc
不提供此功能。
Rc
为您提供共享所有权。内部值有多个所有者,只要至少有一个所有者仍然持有它,引用计数就可以确保数据保持事件状态。如果您的数据没有明确的单一所有者,这将非常有用。
RefCell
为您提供内部可变性,即您可以在运行时动态借用内部值,即使使用共享引用也可以修改它。
Rc<RefCell<...>>
组合为您提供了两者的组合 - 一个具有多个所有者的值,可以由任何一个所有者可变地借用。
Rc<T>
, the Reference Counted Smart Pointer RefCell<T>
and the Interior Mutability Pattern 关于rust - 了解 Rc<RefCell<SomeStruct>> 在 Rust 中的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61997859/
我定义了 3 个结构和两种类型 type OnMoveEvent = fn(Board) -> (); type OnGameOverEvent = fn(Player) -> (); #[wasm_
假设 SomeStruct 定义为: struct SomeStruct { int member; }; 这些是什么意思? &SomeStruct::member int SomeStruc
const Vector4 colorBlack = new Vector4(0,0,0,1);//Vector4 is struct public static void example(Vecto
考虑以下几点: struct SomeStruct {} var foo: Any! let bar: SomeStruct = SomeStruct() foo = bar // Compiles
考虑以下几点: struct SomeStruct {} var foo: Any! let bar: SomeStruct = SomeStruct() foo = bar // Compiles
我试图理解 ORM 库的这段代码,但我无法理解 (*User)(nil) 的含义?第一个括号是指向 User 结构的指针,那么第二个括号代表什么? type User struct { Id
我想获取结构中特定成员的大小。 sizeof(((SomeStruct *) 0)->some_member) 对我有用,但我觉得可能有更好的方法。 我可以#define SIZEOF_ELEM(ST
我不小心输入了一个默认的可初始化对象 struct至 std::numeric_limits::infinity() .我得到的是一个默认结构。 为什么标准允许它编译并返回这样一个意想不到的值? #i
我有一个或多或少像这样的结构: [Serializable] [XmlRoot("Customer")] public struct TCustomer { string CustomerNo;
假设我有很多带有接收器的函数或方法,每个函数或方法都有不同类型的参数。我想使用表驱动方法来调度函数或方法调用。所以我将构建一个这样的表: type command struct { name s
我正在查看一些使用的代码 Rc> 所以我出去阅读了 Rc 和 RefCell 之间的差异: 以下是选择 Box、Rc 或 RefCell 的原因的概述: Rc enables multiple own
我在这里做了一些比较: https://github.com/itchingpixels/structs-vs-classes似乎将结构插入结构数组比将类插入类数组(具有相同数据)慢 10 倍。 我的
例如: type name struct { name string age int } func main() { c := make(chan name)
给定 SomeStruct 如下: struct SomeStruct { int i; char c; }; 下面不编译。 int main() { std::array a
谁能解释一下这个错误是什么意思?这实际上是相同的结构。它是否无法以某种方式推断出 R 或 F 并以这种方式报告? src/demo.rs:113:51: 113:65 error: mismatche
主题。用例是:假设我有一个非托管缓冲区,用于存储 SomeStruct 的 N 个实例。 .所以在这个缓冲区中有一个地址,有没有办法把这个地址转换成ref SomeStruct -- 例如能够直接更新
这里有一些设置代码来解释正在发生的事情: protocol CanJump{ func jump() } struct Dog: CanJump{ func jump(){
我有一个结构 type mapKey string var key1 mapKey = "someKey" var key2 mapKey = "anotherKey" type SampleMap
当我尝试使用用户定义的转换运算符从接口(interface)类型转换为通用结构类型时,出现编译错误,指出类型无法转换: public interface J { } public struct S {
我是一名优秀的程序员,十分优秀!