【发布时间】:2023-01-09 07:02:21
【问题描述】:
我写了一个函数来了解 Rust 中的 Option:
fn f1() {
let a: Option<i32> = Some(1);
let b: Option<&i32> = a.as_ref();
let c: Option<i32> = b.copied(); // I thought b is moved.
println!("{:?}", b); // use a reference of b, why no errors?
}
我发现copied的签名是这样的,所以应该使用Option<&T>:
impl<T> Option<&T> {
pub fn copied(self) -> Option<T>
where
T: Copy
...
我想这是因为全面实施:
impl<T> Copy for Option<T>
where
T: Copy,
我知道 i32 实现了 Copy(在 std document 中),但我没有找到任何关于 &i32 实现 Copy 的信息。有人可以帮助我吗?
【问题讨论】:
-
来自
std::marker::Copy,它显示为“共享引用 (&T) 也是复制,所以 [...]”。
标签: rust