Trait std::borrow::ToOwned

1.0.0 · source ·
pub trait ToOwned {
    type Owned: Borrow<Self>;

    // Required method
    fn to_owned(&self) -> Self::Owned;

    // Provided method
    fn clone_into(&self, target: &mut Self::Owned) { ... }
}
Expand description

Clone 对借用数据的泛化。

某些类型通常可以通过实现 Clone trait 从借用变为拥有。 但是 Clone 仅适用于从 &TT 的情况。 ToOwned trait 泛化 Clone 来构造给定类型的任何借用数据。

Required Associated Types§

source

type Owned: Borrow<Self>

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

Required Methods§

source

fn to_owned(&self) -> Self::Owned

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

Examples

基本用法:

let s: &str = "a";
let ss: String = s.to_owned();

let v: &[i32] = &[1, 2];
let vv: Vec<i32> = v.to_owned();
Run

Provided Methods§

1.63.0 · source

fn clone_into(&self, target: &mut Self::Owned)

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

这是 Clone::clone_from 泛化的借用版本。

Examples

基本用法:

let mut s: String = String::new();
"hello".clone_into(&mut s);

let mut v: Vec<i32> = Vec::new();
[1, 2][..].clone_into(&mut v);
Run

Implementors§

source§

impl ToOwned for str

1.3.0 · source§

impl ToOwned for CStr

source§

impl ToOwned for OsStr

source§

impl ToOwned for Path

source§

impl<T> ToOwned for [T]where T: Clone,

§

type Owned = Vec<T, Global>

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T