Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do return expressions use semicolons when they're unnecessary?

Tags:

rust

I'm learning Rust and I've found something confusing with functions. According to the official reference, a return expression:

.. [is] denoted with the keyword return. Evaluating a return expression moves its argument into the output slot of the current function, destroys the current function activation frame, and transfers control to the caller frame.

So, this program works:

fn main() {
    let current_hour = 10;
    let message = get_message(current_hour);

    println!("Good {0}", message);
}

fn get_message(current_hour: i32) -> &'static str {

    if current_hour < 11 {
        return "Morning"
    }
    else if current_hour < 17 {
        return "Afternoon"
    }
    else {
        return "Evening"
    }

}

And when I add semi-colons to the "return" expressions, it still works:

fn main() {
    let current_hour = 10;
    let message = get_message(current_hour);

    println!("Good {0}", message);
}

fn get_message(current_hour: i32) -> &'static str {

    if current_hour < 11 {
        return "Morning";
    }
    else if current_hour < 17 {
        return "Afternoon";
    }
    else {
        return "Evening";
    }

}

My understanding of expression statements (e.g. expr;) is that it will evaluate the expr expression, and ignore the result (instead it will use ()). In the case of using return expr;, there doesn't seem to be a reason for using ; since return expr destroys the current function activation frame (and would then ignore the ;).

So, why does a lot of Rust code that I've seen use the semi-colon if it's not necessary (and in fact makes learning about Rust's functions very confusing... since it feels like it's contradictory). Is it just an idiom from other languages that has carried over?

like image 433
TheCloudlessSky Avatar asked Jan 10 '15 13:01

TheCloudlessSky


People also ask

Why do we use a semicolon at the end of a line?

Semicolons aren’t used at the end of the line. They are rather used at the end of statements/expressions. Some languages like C, C++,Java (to name the popular ones) use semicolon for the purpose stated above - “ to separate statements ”.

Why are periods used instead of semicolons in programming languages?

Some languages do not require a semicolon, such as Python or Kotlin. In most languages, even with those where a semicolon is required, sometimes you can omit it and the program will work just fine. For Why are periods used to end a statement in English language—and many of the similar dialect? It is just the way that a language was designed.

What is the difference between a colon and a semicolon?

The semicolon is the colon's quirkier sibling. While the colon is simply two dots stacked : the semicolon is a dot hovering over a comma ; The semicolon does jobs that are also done by other punctuation marks, but puts its own spin on the task. Like a comma, it can separate elements in a series.

Is it bad to put a semicolon after an IF statement?

It won't harm to put a semicolon after the { } of an if statement (it will be ignored, and you might see a warning that it's unnecessary). But a semicolon where it doesn't belong (such as after the round ( brackets) of an if, for, while, or switch statement) is a very bad idea:


1 Answers

Is it just an idiom from other languages that has carried over?

Yes, I think that's it, just habit and possibly a general sense of aesthetics about what feels weird and what doesn't (which is of course influenced by someone's previous languages).

AFAICT, the only difference it can make is for something like

fn foo() {
    return;
    println!("hi");
}

where return needs to be a statement... but the code after the return is unreachable (which the compiler will tell you), so this likely doesn't occur that much in real code.

like image 153
huon Avatar answered Sep 29 '22 12:09

huon