Skip to content

Return expressions without a semicolon compile #26425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pmarcelll opened this issue Jun 19, 2015 · 2 comments
Closed

Return expressions without a semicolon compile #26425

pmarcelll opened this issue Jun 19, 2015 · 2 comments

Comments

@pmarcelll
Copy link
Contributor

My "investigation": #17257 (comment)

Test case:

fn foo() -> i32 {
    return 1 // no semicolon
}

fn main() {
    println!("{}", foo());
}

This compiles, but the reference doesn't mention, that is should.

@Aatch
Copy link
Contributor

Aatch commented Jun 19, 2015

The reference also doesn't say it shouldn't. This is expected behaviour as it allows this to be valid Rust code:

let x = match foo {
    1 => do_1(),
    2 => do_2(),
    _ => return 0
}

Since a semicolon here wouldn't be valid syntax. Note that the semicolon isn't part of the return expression at all, it is instead part of statement syntax.

Referencing your comment in the linked issue, there are many constructs that you're unlikely to see in regular Rust code that are, nonetheless, valid. Take a look at this test for some crazy stuff that is perfectly valid Rust code.

@Aatch Aatch closed this as completed Jun 19, 2015
@pmarcelll
Copy link
Contributor Author

Ok, thanks. It really makes sense now (and it makes things a bit more flexible and ergonomic), but I think it should be mentioned in TRPL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants