fn main()
{
println!("114514:");
let d = 114514;
let c = 1919810;
let shit = senpai(d,c);
if shit == 100000 {
println!("新鲜的很那");
} else {
println!("先辈,你没拉答辩(悲");
}
}
fn senpai(a:i32,b:i32) -> i32 {
let aa = a;
let bb = b;
let al = 114514;
let aaa = 1919810;
if aa == al
{
println!("你不是一个一个一个");
}
else
{
if bb == aaa
{
println!("你不是一个一个一个");
}
else
{
println!("你是一个一个一个,啊,啊啊啊");
return 100000;
}
}
}
Your senpai() function should return an i32 in all code branches. If you don't do this there will be an error. However, you only return a value (100000) if aa != al and bb != aaa.
You either need to move the "return 100000" statement outside the outer if block or you need to return values after every println! statement.
Based on the automatically translated text inside the println! calls, it is likely that you don't want to return anything sometimes. Some languages, like Python or JavaScript, implicitly return null or a similar value if you don't return anything explicitly. However, this behavior is often not wanted or desired and leads to subtle, hard-to-find bugs. That's why Rust has the Option<T> enum that either contains a value or none, i.e. is empty. Idiomatically, your code could be rewritten to look like this:
fn main() {
println!("114514:");
let d = 114514;
let c = 1919810;
let shit = senpai(d, c);
if shit == Some(100000) {
println!("新鲜的很那");
} else {
println!("先辈,你没拉答辩(悲");
}
}
fn senpai(a: i32, b: i32) -> Option<i32> {
let al = 114514;
let aaa = 1919810;
if a == al || b == aaa {
println!("你不是一个一个一个");
None
} else {
println!("你是一个一个一个,啊,啊啊啊");
Some(100000)
}
}
This code can also be accessed and run via the Rust Playground, an online tool to compile, run, test and share Rust programs.
Here's a list of what I changed:
Formatted the code (in the Playground, click Tools > Format to make it more readable and understandable instantly)
Changed the signature of senpai to return an optional value (which fixes the bug), return None in the general case and the number in the special case
Inlined the bindings aa and bb - you can just use a and b directly
Collapsed the else { if {} } block into an else if {} block
Collapsed this block too because the contents were identical. The new code is using the || (logical or) operator - if a is equal to al or b is equal to aaa, do what's inside the block
What still needs to be done?
More descriptive variable names: no one reading your code will know what a, b, c, d, aaa or al mean. You should name your variables according to what they refer to, e.g. total_money, item_count or window_width.
I hope you'll find this advice valuable and I wish you good luck on your journey to learn the Rust language!