【发布时间】:2022-06-22 23:08:06
【问题描述】:
在 rust 中,我可以在模板参数上设置 trait bound,以保证它符合我想要的功能:
fn print<T:Debug>(t: T) {
println!("{:?}", t);
}
我可以对字段做类似的事情吗?
fn print_name<T:HasNameField>(t: T) {
println!("{:?}", t.name);
}
我的用例是在 yew 中我想创建一个表单。而不仅仅是使用<input type="text".../>,我希望用户能够创建自己的输入字段并能够构建CustomForm。然后我可以:
#[function_component(CustomForm)]
fn custom_form<T: yew::Component>() -> Html {
<form>
<T name="field name"/>
</form>
}
目前此操作失败并显示以下消息:
error[E0609]: no field `name` on type `<T as yew::Component>::Properties`
【问题讨论】: