我通过查看和操纵其他人的例子来学习Rust . 我试图将以下代码封装到结构中:
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
我想创建一个像这样的结构:
pub struct Window { encoder: gfx::Encoder<?, ?>, // ... }
我如何知道我必须在问号中加入哪些类型?
更改变量的类型( encoder )以导致类型不匹配 . 最简单的类型是 () :
encoder
()
let mut encoder: gfx::Encoder<(), ()> = factory.create_command_buffer().into();
这将生成具体类型的错误,然后您可以直接清理和使用 .
另见How do I print the type of a variable in Rust? .
在许多情况下,你会使用更简单的东西:
let mut encoder: () = factory.create_command_buffer().into();
但这不太可能在这种特定情况下起作用,因为 into 具有多态返回类型 . 它需要指定一些具体类型才能知道应该调用哪个实现 .
into
1 回答
更改变量的类型(
encoder
)以导致类型不匹配 . 最简单的类型是()
:这将生成具体类型的错误,然后您可以直接清理和使用 .
另见How do I print the type of a variable in Rust? .
在许多情况下,你会使用更简单的东西:
但这不太可能在这种特定情况下起作用,因为
into
具有多态返回类型 . 它需要指定一些具体类型才能知道应该调用哪个实现 .