trait_ToOwned

trait_ToOwned (소유 타입으로의 변환 트레이트)

빌린(borrowed) 데이터를 소유된(owned) 데이터로 변환하는 일반화된 트레이트예요.

출처: Rust 공식 문서

본문

ToOwnedClone과 비슷하지만, 빌린 값에서 소유된 값을 만들 때 사용돼요. 예를 들어 strString으로, [T]Vec<T>로 변환할 수 있어요.

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

    // 필수 메서드
    fn to_owned(&self) -> Self::Owned;

    // 제공되는 메서드
    fn clone_into(&self, target: &mut Self::Owned) { ... }
}

예시

let s: &str = "hello";
let owned: String = s.to_owned();

더 알아보기 (Learn more)

Rust 공식 문서