Guest User

Untitled

a guest
Jul 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. struct RGB { red: f64, green: f64, blue: f64 }
  2.  
  3. /*
  4. fn main() {
  5. let (red, green, blue): (f32, f32, f32) = (0., 0., 0.);
  6. let _ = RGB { red, green, blue };
  7. }
  8. */
  9.  
  10. // This gives a bad/misleading error message
  11.  
  12. /*
  13.  
  14. error[E0308]: mismatched types
  15. --> src/main.rs:6:19
  16. |
  17. 6 | let _ = RGB { red, green, blue };
  18. | ^^^ expected f64, found f32
  19. help: you can cast an `f32` to `f64` in a lossless way
  20. |
  21. 6 | let _ = RGB { red.into(), green, blue };
  22. | ^^^^^^^^^^
  23.  
  24. // But, if I do try with that .into(), the error message is even worse!
  25.  
  26. error: expected one of `,` or `}`, found `.`
  27. --> src/main.rs:6:22
  28. |
  29. 6 | let _ = RGB { red.into(), green, blue };
  30. | ^ expected one of `,` or `}` here
  31.  
  32. error[E0308]: mismatched types
  33. --> src/main.rs:6:19
  34. |
  35. 6 | let _ = RGB { red.into(), green, blue };
  36. | ^^^ expected f64, found f32
  37. help: you can cast an `f32` to `f64` in a lossless way
  38. |
  39. 6 | let _ = RGB { red.into().into(), green, blue };
  40. | ^^^^^^^^^^
  41.  
  42. error[E0063]: missing fields `blue`, `green` in initializer of `RGB`
  43. --> src/main.rs:6:13
  44. |
  45. 6 | let _ = RGB { red.into(), green, blue };
  46. | ^^^ missing `blue`, `green`
  47.  
  48. error: aborting due to 3 previous errors
  49.  
  50. */
  51.  
  52. // The expected way to fix this issue is:
  53.  
  54. fn main() {
  55. let (red, green, blue): (f32, f32, f32) = (0., 0., 0.);
  56. let _ = RGB { red: red.into(), green: green.into(), blue: blue.into() };
  57. }
  58.  
  59. // Which is EXTREMELY, unnecesarily verbose...
  60. // I wish there was a better way, but the best so far is to
  61. // have an explicit conversion function.
  62. // (And you can't write a From impl if you don't own the RGB type)
Add Comment
Please, Sign In to add comment