Hello guys, I need more clarification on Rust type.
I tried to assign a u8 type variable to u32 type variable but i got compilation errors, more recently this one:
cannot subtract u8 from u32.
My thought was that all integer should substract without issues.
Please clarify.
You will need to explicitly cast to a common integer type, like so
let x: u8 = ...;
let y: u32 = ...;
let z = u32::from(x) - y;
Rust won't do this for you. Note: you can't losslessly cast u32 to u8, so you should use u32::try_from. This requires the std::convert::TryFrom trait. If you want to truncate the u32 to a u8, use as, y as u8
if show_tips == "Yes" should work - try doing if dbg!(show_tips) == "Yes" (which will log out the value of show_tips), and make sure the output is what you expect.
I see that show_tips contain "Yes\r\n", but when i tried to trim it using show_tips.trim() == "Yes", I get error:
thread 'main' panicked at 'attempt to subtract with overflow'.
Are you getting show_tips using read_line or lines, or something like that? If so, that will always include the line delimiter at the end of the string, so trimming is the right thing to do there.
I don't believe trim would ever panic with an overflow though - that error is probably coming from somewhere else in your program. Are you subtracting a number somewhere?
If you can't figure it out, set the RUST_BACKTRACE environment variable to 1. This will give you a stack trace that will tell you where the panic occurred.
Thanks @17cupsofcoffee It's not the trim() causing the error.
This if statement passed, but the code block inside failed.
But I have resolved it.
Everything is working well now.