2
특성 경계가있는 제네릭 형식의 필드를 사용하여 구조체를 구현하려고합니다. Group.admin
의 값을 Printable
으로 지정하고 싶습니다. 정확히 어떤 유형인지는 중요하지 않습니다.특성 경계가있는 일반 구조체 필드
struct Group<T: Printable> {
admin: T,
}
struct Person {
name: String,
}
impl Person {
fn new() -> Person {
Person {
name: String::from("Bob"),
}
}
}
trait Printable {
fn print(&self);
}
impl Printable for Person {
fn print(&self) {
println!("{}", self.name);
}
}
fn group_test<T: Printable>() -> Group<T> {
Group {
admin: Person::new(),
}
}
그러나 컴파일러는 이것을 허용하지 않습니다
error[E0308]: mismatched types
--> src/lib.rs:27:16
|
27 | admin: Person::new(),
| ^^^^^^^^^^^^^ expected type parameter, found struct `Person`
|
= note: expected type `T`
found type `Person`
여기 뭐가 잘못 됐는지 모르겠어요. 어쩌면 Person::new()
의 가치를 <T: Printable>
에 던져야할까요?