【问题标题】:How do I know what types I have to put into the generic type parameters?我如何知道必须将哪些类型放入泛型类型参数中?
【发布时间】:2016-08-12 04:52:13
【问题描述】:

我通过查看和操作其他人的示例来学习 Rust。我尝试将以下代码封装到一个结构中:

let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();

我想创建一个这样的结构:

pub struct Window {
    encoder: gfx::Encoder<?, ?>,
    // ...
}

我如何知道我必须在问号中输入什么类型?

【问题讨论】:

    标签: rust


    【解决方案1】:

    更改变量的类型 (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 具有多态返回类型。它需要指定一些具体的类型才能知道应该调用哪个实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      相关资源
      最近更新 更多