当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust Arc.try_unwrap用法及代码示例


本文简要介绍rust语言中 std::sync::Arc.try_unwrap 的用法。

用法

pub fn try_unwrap(this: Arc<T>) -> Result<T, Arc<T>>

如果 Arc 恰好有一个强引用,则返回内部值。

否则,返回 Err 和传入的 Arc

即使存在未完成的弱引用,这也会成功。

例子

use std::sync::Arc;

let x = Arc::new(3);
assert_eq!(Arc::try_unwrap(x), Ok(3));

let x = Arc::new(4);
let _y = Arc::clone(&x);
assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 std::sync::Arc.try_unwrap。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。