【问题标题】:Failing to understand a rust generic function error [duplicate]无法理解 rust 通用函数错误 [重复]
【发布时间】:2016-11-14 05:00:46
【问题描述】:

我试图编译一段 Rust 通用代码,经过一段时间的研究,我将其缩小到无法编译这段代码。我不确定这里到底发生了什么(E0308 对我没有多大帮助)——我一定是错过了一些愚蠢的东西:

fn is_fail<bool>() -> bool { false }
fn main(){
  let failure:bool = is_fail();
  //if ! failure {
    println!("{}", failure);
  //}
}

错误是:

error: mismatched types [--explain E0308]
 --> <anon>:1:30
1 |> fn is_fail<bool>() -> bool { false }
  |>                              ^^^^^ expected type parameter, found bool
note: expected type `bool`
note:    found type `bool`

【问题讨论】:

    标签: rust


    【解决方案1】:

    在您的函数中,类型参数bool 隐藏了内置类型bool。所以你的函数声明本质上是一样的

    fn is_fail<T>() -> T { false }
    

    这显然不是很好的类型。

    【讨论】:

    • 是的,但现在很清楚为什么它是错误的。 false 不是泛型类型T
    • 是的..类型“bool”被类型参数“bool”遮蔽!
    猜你喜欢
    • 2013-03-11
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多