trait_ToOwned
trait_ToOwned (소유 타입으로의 변환 트레이트)
빌린(borrowed) 데이터를 소유된(owned) 데이터로 변환하는 일반화된 트레이트예요.
출처: Rust 공식 문서
본문
ToOwned는 Clone과 비슷하지만, 빌린 값에서 소유된 값을 만들 때 사용돼요. 예를 들어 str은 String으로, [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();