【发布时间】:2016-07-10 18:00:17
【问题描述】:
我试图让这段代码更简洁,并去掉多余的变量。有可能吗?
trait Foo {}
impl Foo for i32 {}
impl Foo for String {}
fn main() {
let xi32: i32 = 10;
let y = String::from("ooo");
let mut foo_list: Vec<&Foo> = vec![];
foo_list.push(&xi32 as &Foo);
foo_list.push(&y as &Foo);
}
以下变体不起作用:
foo_list.push(10 as Foo);
error[E0620]: cast to unsized type: `{integer}` as `Foo`
--> src/main.rs:11:19
|
11 | foo_list.push(10 as Foo);
| ^^^^^^^^^
|
help: consider using a box or reference as appropriate
--> src/main.rs:11:19
|
11 | foo_list.push(10 as Foo);
| ^^
【问题讨论】:
标签: rust