Skip to content

Implicit return of last expression doesn't work #1194

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
boggle opened this issue Nov 19, 2011 · 5 comments
Closed

Implicit return of last expression doesn't work #1194

boggle opened this issue Nov 19, 2011 · 5 comments

Comments

@boggle
Copy link
Contributor

boggle commented Nov 19, 2011

According to the tutorial, fn foo()->int { 7 } should be equivalent to fn foo()->int { ret 7; }. However, rustc right now rejects such function definitions.

@brson
Copy link
Contributor

brson commented Nov 19, 2011

fn foo()->int { 7 } works for me. Indeed this is style of function is used frequently in rust. Maybe there is more to the error you are seeing.

@boggle
Copy link
Contributor Author

boggle commented Nov 19, 2011

The line in question was

   let g = vec::init_fn({|_i| ret vec::init_elt_mut(0 as u8, 10u); }, 10u);

Removing the ret gave type errors further down the function ("the type of this value must be known in this context"). I was blaming the ret. Adding an explicit type to g (still no ret) gives "In function anon, not all control paths return a value". Turns out that the trailing ";" is the cultprit, although I don't see why, and

let g = vec::init_fn({|_i| vec::init_elt_mut(0 as u8, 10u) }, 10u);

just works fine (no ret, no ; in the block) and doesn't require an explicit type annotation.

@lht
Copy link
Contributor

lht commented Nov 19, 2011

I think this is covered in the expression syntax section of the tutorial

@boggle
Copy link
Contributor Author

boggle commented Nov 19, 2011

so it becomes nil. Well, a better error message or perhaps a warning would be nice in such cases.

@boggle boggle closed this as completed Nov 19, 2011
@brson
Copy link
Contributor

brson commented Nov 19, 2011

I think pcwalton proposed at some point a way to make the trailing semi optional. I've heard a few complaints about this syntax. A warning would definitely be useful. I'll open another issue for that.

Kobzol pushed a commit to Kobzol/rust that referenced this issue Dec 30, 2024
bors pushed a commit to rust-lang-ci/rust that referenced this issue Jan 2, 2025
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

3 participants