Failing to understand a rust generic function erro

2019-07-24 19:36发布

This question already has an answer here:

I was trying to get a piece of Rust generic code to compile, and after mucking around for while narrowed it down to failure of this code to compile. I am not sure what exactly is breaking down here (E0308 doesn't help me much) -- I must be missing something silly:

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

The error is:

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条回答
再贱就再见
2楼-- · 2019-07-24 20:00

In your function the type parameter bool shadows the builtin type bool. So your function declaration is essentially the same as

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

which clearly isn't well-typed.

查看更多
登录 后发表回答