This approach definitely makes sense for teaching purposes. I managed to use a similar approach without using any allocation (no Strings, no Vecs). In Rust 1.46 it could even be implemented as a const function relatively easy. But if you're allocating anyway, you might as well collect into a String and use String::parse. Then the whole thing becomes a oneliner :D -> x.abs().to_string().chars().rev().collect::().parse::().map(|r| r * x.signum()).unwrap_or(0)
Hi, great tutorial, I have just started learning rust, through rust book and some of your tutorials, i am upto the 10th chapter an I don't think i have seen "10_i32" thing anywhere. Can you please explain or point me to some resource ?
Thanks! To answer your question, "10_i32" is shorthand for "10 as i32". Without the explicit type, the compiler will complain with: "can't call method `pow` on ambiguous numeric type `{integer}`" ~ hope that clarifies it!
Upon further investigation, I think I might have messed up when exporting the video 🤦♂️ ~ I’m not sure if I can fix this as it is already uploaded but I’ll do my best to not make this mistake again and, while the quality is bad, I hope you can still enjoy the content :/
fn reverse_integer(x: i32) -> i32 { let sign = if x >= 0 { 1 } else { -1 }; let mut x = x * sign; let mut digit = x % 10; let mut result: i32 = 0; while x > 0 { result = result * 10 + digit; x = x / 10; digit = x % 10; } result * sign }
This approach definitely makes sense for teaching purposes. I managed to use a similar approach without using any allocation (no Strings, no Vecs). In Rust 1.46 it could even be implemented as a const function relatively easy. But if you're allocating anyway, you might as well collect into a String and use String::parse. Then the whole thing becomes a oneliner :D ->
x.abs().to_string().chars().rev().collect::().parse::().map(|r| r * x.signum()).unwrap_or(0)
Ooo, that's pretty cool! I'd love to see your other approach too ;D
Hi, great tutorial, I have just started learning rust, through rust book and some of your tutorials, i am upto the 10th chapter an I don't think i have seen "10_i32" thing anywhere. Can you please explain or point me to some resource ?
Thanks!
To answer your question, "10_i32" is shorthand for "10 as i32". Without the explicit type, the compiler will complain with: "can't call method `pow` on ambiguous numeric type `{integer}`" ~ hope that clarifies it!
You'll also sometimes see syntax like '10i32' or '10u8'
@@danlogs thanks
Only 480p available? Or is the video still being processed?
Oof, thanks for bringing this to my attention. It should just be processing - Still, I’ll check back on this later to make sure 👌
danlogs Still 480p for me too
Upon further investigation, I think I might have messed up when exporting the video 🤦♂️ ~ I’m not sure if I can fix this as it is already uploaded but I’ll do my best to not make this mistake again and, while the quality is bad, I hope you can still enjoy the content :/
Fixed in my latest upload: th-cam.com/video/wbCMbQ1rYXE/w-d-xo.html ✅
fn reverse_integer(x: i32) -> i32 {
let sign = if x >= 0 { 1 } else { -1 };
let mut x = x * sign;
let mut digit = x % 10;
let mut result: i32 = 0;
while x > 0 {
result = result * 10 + digit;
x = x / 10;
digit = x % 10;
}
result * sign
}
Just don't forget to check for overflow! Try out values like 1534236469 when testing your program ^^
@@danlogs if i pass value larger than i32 to function, compiler will bite my ass