robn

Untitled

Jan 17th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. robn@starmoth:~/code/rust/bldump$ cargo run
  2. Compiling bldump v0.1.0 (file:///home/robn/code/rust/bldump)
  3. src/main.rs:394:26: 394:36 error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282]
  4. src/main.rs:394 let stored_crc = rdr.read_u32().unwrap();
  5. ^~~~~~~~~~
  6. src/main.rs:394:26: 394:36 help: run `rustc --explain E0282` to see a detailed explanation
  7. error: aborting due to previous error
  8. Could not compile `bldump`.
  9.  
  10. To learn more, run the command again with --verbose.
  11. robn@starmoth:~/code/rust/bldump$ rustc --explain E0282
  12. This error indicates that type inference did not result in one unique possible
  13. type, and extra information is required. In most cases this can be provided
  14. by adding a type annotation. Sometimes you need to specify a generic type
  15. parameter manually.
  16.  
  17. A common example is the `collect` method on `Iterator`. It has a generic type
  18. parameter with a `FromIterator` bound, which for a `char` iterator is
  19. implemented by `Vec` and `String` among others. Consider the following snippet
  20. that reverses the characters of a string:
  21.  
  22. ```
  23. let x = "hello".chars().rev().collect();
  24. ```
  25.  
  26. In this case, the compiler cannot infer what the type of `x` should be:
  27. `Vec<char>` and `String` are both suitable candidates. To specify which type to
  28. use, you can use a type annotation on `x`:
  29.  
  30. ```
  31. let x: Vec<char> = "hello".chars().rev().collect();
  32. ```
  33.  
  34. It is not necessary to annotate the full type. Once the ambiguity is resolved,
  35. the compiler can infer the rest:
  36.  
  37. ```
  38. let x: Vec<_> = "hello".chars().rev().collect();
  39. ```
  40.  
  41. Another way to provide the compiler with enough information, is to specify the
  42. generic type parameter:
  43.  
  44. ```
  45. let x = "hello".chars().rev().collect::<Vec<char>>();
  46. ```
  47.  
  48. Again, you need not specify the full type if the compiler can infer it:
  49.  
  50. ```
  51. let x = "hello".chars().rev().collect::<Vec<_>>();
  52. ```
  53.  
  54. Apart from a method or function with a generic type parameter, this error can
  55. occur when a type parameter of a struct or trait cannot be inferred. In that
  56. case it is not always possible to use a type annotation, because all candidates
  57. have the same return type. For instance:
  58.  
  59. ```
  60. struct Foo<T> {
  61. // Some fields omitted.
  62. }
  63.  
  64. impl<T> Foo<T> {
  65. fn bar() -> i32 {
  66. 0
  67. }
  68.  
  69. fn baz() {
  70. let number = Foo::bar();
  71. }
  72. }
  73. ```
  74.  
  75. This will fail because the compiler does not know which instance of `Foo` to
  76. call `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.
Add Comment
Please, Sign In to add comment