r/rustjerk • u/RedstoneTier • Oct 14 '26
Why is my good looking rust code not working?
Hello, imn't new to rust. This rust code looks really good i must say. However, it is literally the opposite of working right now?
Current output:
4 is odd!I just don't understand whats going on... Can some rustishn fix this devious error for me? thanks.
playground link
336
Upvotes
148
u/karasawa_jp Oct 14 '26
43
u/TinkmasterOverspark Oct 14 '26
You forgot to use unsafe
7
u/borisko321 Oct 14 '26
Did you try rewriting it in perl?
3
u/Sky_Klokwork Oct 14 '26
Not sure if this is one of those joke subs but seemingly inverting your test case `n % 2 != 0` should do it since odd numbers tend to have a remainder when modulo 2 is used on them.
In other news, found out about a feature I didn’t know. A bit of a strange way of doing it, but its kinda cool you can write it this way
5
u/fl_needs_to_restart Oct 14 '26
You need
{} elseafter areturn ifstatement:https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=2f4eb92ba5f14e74c894189ce67f332b
112
u/Valuable_Leopard_799 Oct 14 '26
Lgtm, 4 is quite an odd number, as opposed to 3 which is completely normal.
59
u/AdreKiseque Oct 14 '26
Must be a compiler bug.
25
u/akshay-nair Oct 14 '26
Ruby dev inventing rust syntax
14
u/SmoothTurtle872 Oct 14 '26
I didn't read the sub, and almost gave actual advice
3
u/Dependent_Tart_2413 Oct 15 '26
Is this a trolling? So stupid question and so much comments. Maybe u want something like 'if n % 2 != 0'
18
u/Thelatestart Oct 14 '26
I'm a cpp dev, so i googled 'rust "return if"' and found it doesnt exist... which means the statement is literally doing (return(if ...)) its like returning the if XD? And so your if doesnt apply to the return, but the braces after, so you only print when n%2==0
7
u/Antagonin Oct 14 '26
I legitimately can't tell if that's valid rust or not.
5
u/Ok-Argument7176 Oct 14 '26
They recently changed the definition of odds, so I think it's working as intended. Have you run on nightly to confirm?
5
u/SpeckyYT Oct 15 '26
bro discovered CoffeeScript syntax
4
u/denehoffman Oct 14 '26
/uj you should use `is_multiple_of`, it’s cleaner
/rj off-by-one errors like this happen because rust is 0-indexed, don’t worry about it
4
u/AdOnly69 Oct 14 '26
You should consider using c++ with gcc, rust compiler is kinda clangy
3
u/morglod Oct 14 '26
/uj Please don't say that body in that condition should work on false branch
3
u/Azra__1 Oct 14 '26
What is the point of the return here?
3
u/avg_bndt Oct 14 '26
Needs more generics
3
u/DragonSnooz Oct 14 '26
It's working as written:
If (n % 2 == 0) // true when even
{
println!("{n} is odd);
}
every time you have an even number, you will be calling it odd.
3
u/SmoothTurtle872 Oct 14 '26
Unfixable, change it to an even number alerter, then you will know if you input an odd number because it won't trigger
5
u/stumpychubbins Oct 14 '26
Sorry for not using a code block, I’m on mobile. This is parsed as "return (if (n % 2 == 0) { println!(..); })". You’re returning the value of that if statement. Any if statement without an else will have an implicit "else { () }", which will usually cause a type error if you use if as an expression without an else. In this case, though, the true branch also returns (), as that is the implicit return of blocks ending with ;. This means that instead of returning if the condition is true, you’re executing the println if the condition is true
Unlike some languages, rust doesn’t have "return if" as a dedicated syntactic construct. You need to do "if condition { return; }"
2
u/Most_Strike_93 29d ago
Is it rubyrust?
2
u/MistRider-0 Oct 14 '26 edited Oct 14 '26
Fixed!
2
u/riisen Oct 15 '26
2
u/DangyDanger Oct 15 '26
Don't thank me
2
u/andrewprograms Oct 15 '26
Pop another “bubble gum” (Zyn, assuming Palantir because of the defense nature of that algorithm)
2
u/catlifeonmars Oct 16 '26
I think you meant:
return unless n.odd?
Also don’t forget to wrap the whole thing in begin… rescue
2
u/monta_gia Oct 19 '26
what expectation result you want?
if you expect 3 to be printed also then will never happen
2
u/FlameyosFlow 23d ago
Well, that's because you didn't represent it in a rust enum, of course it won't be type safe rust code.
It should really be:
Now your code is using a high performance, memory safe, type safe and elegant way to check if a number is even or odd :)
If you want to improve on this, you can even use the type state pattern so that you double ensure that a number can never be anything other than odd or even!
1
u/AcolyteArathok Oct 15 '26
4%2 is 0! Odd is when %2 returns a 1 Because %does the division and then returnst the rest.
1
u/TylerBreau Oct 17 '26
.... 8 years full stack dev, never used Rust before. Not familiar with the syntax but looking at what you've said...
if n % 2 == 04 % 2 == 03 % 2 != 0Odd numbers fail your condition. I assume the {} block only runs if the condition passes. Or maybe you're missing an else statement but unless you want run code for the even case, I would just change the if-condition.
1
u/TheChief275 Oct 14 '26
'good looking' and 'rust code' are antitheses
5
u/regeya Oct 14 '26
Oh, that is evil. You know a lot of us have had our brains rotted out by Python