Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct RGB { red: f64, green: f64, blue: f64 }
- /*
- fn main() {
- let (red, green, blue): (f32, f32, f32) = (0., 0., 0.);
- let _ = RGB { red, green, blue };
- }
- */
- // This gives a bad/misleading error message
- /*
- error[E0308]: mismatched types
- --> src/main.rs:6:19
- |
- 6 | let _ = RGB { red, green, blue };
- | ^^^ expected f64, found f32
- help: you can cast an `f32` to `f64` in a lossless way
- |
- 6 | let _ = RGB { red.into(), green, blue };
- | ^^^^^^^^^^
- // But, if I do try with that .into(), the error message is even worse!
- error: expected one of `,` or `}`, found `.`
- --> src/main.rs:6:22
- |
- 6 | let _ = RGB { red.into(), green, blue };
- | ^ expected one of `,` or `}` here
- error[E0308]: mismatched types
- --> src/main.rs:6:19
- |
- 6 | let _ = RGB { red.into(), green, blue };
- | ^^^ expected f64, found f32
- help: you can cast an `f32` to `f64` in a lossless way
- |
- 6 | let _ = RGB { red.into().into(), green, blue };
- | ^^^^^^^^^^
- error[E0063]: missing fields `blue`, `green` in initializer of `RGB`
- --> src/main.rs:6:13
- |
- 6 | let _ = RGB { red.into(), green, blue };
- | ^^^ missing `blue`, `green`
- error: aborting due to 3 previous errors
- */
- // The expected way to fix this issue is:
- fn main() {
- let (red, green, blue): (f32, f32, f32) = (0., 0., 0.);
- let _ = RGB { red: red.into(), green: green.into(), blue: blue.into() };
- }
- // Which is EXTREMELY, unnecesarily verbose...
- // I wish there was a better way, but the best so far is to
- // have an explicit conversion function.
- // (And you can't write a From impl if you don't own the RGB type)
Add Comment
Please, Sign In to add comment