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


Rust Cow.into_owned用法及代码示例


本文简要介绍rust语言中 std::borrow::Cow.into_owned 的用法。

用法

pub fn into_owned(self) -> <B as ToOwned>::Owned

提取拥有的数据。

如果数据尚未拥有,则克隆数据。

例子

Cow::Borrowed 上调用 into_owned 会克隆基础数据并变为 Cow::Owned

use std::borrow::Cow;

let s = "Hello world!";
let cow = Cow::Borrowed(s);

assert_eq!(
  cow.into_owned(),
  String::from(s)
);

Cow::Owned 上调用 into_owned 是 no-op:

use std::borrow::Cow;

let s = "Hello world!";
let cow: Cow<str> = Cow::Owned(String::from(s));

assert_eq!(
  cow.into_owned(),
  String::from(s)
);

相关用法


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