Struct std::cell::Cell1.0.0[][src]

#[repr(transparent)]
pub struct Cell<T> where
    T: ?Sized
{ /* fields omitted */ }
Expand description

可变的内存位置。

Examples

在这个例子中,您可以看到 Cell<T> 在一个不可变的结构中实现了变异。 换句话说,它实现了内部可变性。

use std::cell::Cell;

struct SomeStruct {
    regular_field: u8,
    special_field: Cell<u8>,
}

let my_struct = SomeStruct {
    regular_field: 0,
    special_field: Cell::new(1),
};

let new_value = 100;

// ERROR: `my_struct` 是不可变 my_struct.regular_field =new_value;

// WORKS: 尽管 `my_struct` 是不可变的,但是 `special_field` 是 `Cell`,可以随时对其进行修改
my_struct.special_field.set(new_value);
assert_eq!(my_struct.special_field.get(), new_value);
Run

有关更多信息,请参见 模块级文档

Implementations

创建一个包含给定值的新 Cell

Examples
use std::cell::Cell;

let c = Cell::new(5);
Run

设置包含的值。

Examples
use std::cell::Cell;

let c = Cell::new(5);

c.set(10);
Run

交换两个 Cell 的值。 与 std::mem::swap 的区别在于,这个函数不需要 &mut 引用。

Examples
use std::cell::Cell;

let c1 = Cell::new(5i32);
let c2 = Cell::new(10i32);
c1.swap(&c2);
assert_eq!(10, c1.get());
assert_eq!(5, c2.get());
Run

val 替换包含的值,并返回旧的包含的值。

Examples
use std::cell::Cell;

let cell = Cell::new(5);
assert_eq!(cell.get(), 5);
assert_eq!(cell.replace(10), 5);
assert_eq!(cell.get(), 10);
Run

解开包装的值。

Examples
use std::cell::Cell;

let c = Cell::new(5);
let five = c.into_inner();

assert_eq!(five, 5);
Run

返回所包含值的副本。

Examples
use std::cell::Cell;

let c = Cell::new(5);

let five = c.get();
Run
🔬 This is a nightly-only experimental API. (cell_update #50186)

使用函数更新包含的值并返回新值。

Examples
#![feature(cell_update)]

use std::cell::Cell;

let c = Cell::new(5);
let new = c.update(|x| x + 1);

assert_eq!(new, 6);
assert_eq!(c.get(), 6);
Run

返回指向此 cell 中底层数据的裸指针。

Examples
use std::cell::Cell;

let c = Cell::new(5);

let ptr = c.as_ptr();
Run

返回对底层数据的可变引用。

这个调用可变地 (在编译时) 借用了 Cell,这保证了我们拥有唯一的引用。

但是要小心:此方法要求 self 是可变的,而使用 Cell 时通常不是这种情况。 如果您需要通过引用实现内部可变性,可以考虑使用 RefCell,它通过其 borrow_mut 方法提供运行时检查的可变借用。

Examples
use std::cell::Cell;

let mut c = Cell::new(5);
*c.get_mut() += 1;

assert_eq!(c.get(), 6);
Run

&mut T 返回 &Cell<T>

Examples
use std::cell::Cell;

let slice: &mut [i32] = &mut [1, 2, 3];
let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
let slice_cell: &[Cell<i32>] = cell_slice.as_slice_of_cells();

assert_eq!(slice_cell.len(), 3);
Run

获取 cell 的值,将 Default::default() 保留在其位置。

Examples
use std::cell::Cell;

let c = Cell::new(5);
let five = c.take();

assert_eq!(five, 5);
assert_eq!(c.into_inner(), 0);
Run

&Cell<[T]> 返回 &[Cell<T>]

Examples
use std::cell::Cell;

let slice: &mut [i32] = &mut [1, 2, 3];
let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
let slice_cell: &[Cell<i32>] = cell_slice.as_slice_of_cells();

assert_eq!(slice_cell.len(), 3);
Run
🔬 This is a nightly-only experimental API. (as_array_of_cells #88248)

&Cell<[T; N]> 返回 &[Cell<T>; N]

Examples
#![feature(as_array_of_cells)]
use std::cell::Cell;

let mut array: [i32; 3] = [1, 2, 3];
let cell_array: &Cell<[i32; 3]> = Cell::from_mut(&mut array);
let array_cell: &[Cell<i32>; 3] = cell_array.as_array_of_cells();
Run

Trait Implementations

返回值的副本。 Read more

source 执行复制分配。 Read more

使用给定的格式化程序格式化该值。 Read more

创建一个 Cell<T>,使用 T 的 Default 值。

执行转换。

此方法返回 selfother 之间的 OrderingRead more

比较并返回两个值中的最大值。 Read more

比较并返回两个值中的最小值。 Read more

将值限制在某个时间间隔内。 Read more

此方法测试 selfother 值是否相等,并由 == 使用。 Read more

此方法测试 !=

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

Auto Trait Implementations

Blanket Implementations

获取 selfTypeIdRead more

从拥有的值中一成不变地借用。 Read more

从拥有的值中借用。 Read more

执行转换。

执行转换。

执行转换。

获得所有权后的结果类型。

从借用的数据创建拥有的数据,通常是通过克隆。 Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

使用借来的数据来替换拥有的数据,通常是通过克隆。 Read more

发生转换错误时返回的类型。

执行转换。

发生转换错误时返回的类型。

执行转换。