-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add section about diverging functions #960
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
Conversation
Strange, the fail seems to be in this code: fn main() {
fn some_fn() -> () {
return ()
}
let a: () = some_fn();
println!("This functions returns and you can see this line.")
} but it works for me with rustc 1.21, any ideas? |
That is...... very odd. Let me restart it, maybe it's flaky or something? No idea. |
86b8f3f
to
74419e9
Compare
For some reason works like this: fn some_fn() {
()
}
fn main() {
let a: () = some_fn();
println!("This functions returns and you can see this line.")
} |
This is a really cool example and I love seeing this! Given that I don't actually understand diverging functions I gave it a read to try to, well, understand them, so I have a few comments: The never type and it's relationship to the unit type strike me as fairly advanced topics. [also,
I feel this is tough to read. You say it never returns, but then it has a return type. I get what this means, but maybe something like this would read better:
Maybe provide a I had trouble understanding the main example used. I imagine this is the relevant piece of code:
Is it the |
Thanks for comments. I will look into them soon. btw this is the source of information I used: https://github.com/rust-lang/rfcs/blob/master/text/1216-bang-type.md |
Any update on this PR? |
Sorry, I totally forgot about this PR. I submitted a modified version based on the comment above, is it better now? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Thanks so much. No worries about forgetting, it happens to all of us.
I have some small formatting things, and then this is good to go!
src/macro/syntax.md
Outdated
@@ -0,0 +1 @@ | |||
# Syntax |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this was accidentally included
src/fn/diverging.md
Outdated
It is also the return type of functions that loop forever (e.g. `loop {}`) like network servers or functions that terminates the process (e.g. `exit()`). | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you remove these extra blank lines please?
src/fn/diverging.md
Outdated
} | ||
``` | ||
|
||
Although this might seem like an abstract concept, it is in fact very useful and often handy. The main advantage of this type is that it can be cast to any other one and therefore used at places where an exact type is required, for instance in `match` branches. This allows us to write code like this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you wrap this to 80 columns please?
src/fn/diverging.md
Outdated
``` | ||
|
||
As opposed to this function, which will never return the control back to the caller. | ||
```rust |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you put whitespace above this line, to separate the paragraph from the code please?
src/fn/diverging.md
Outdated
As opposed to all the other types, this one cannot be instantiated, because the set of all possible values this type can have is empty. Note, that it is different from the `()` type, which has exactly one possible value. | ||
|
||
For example, this functions returns as usual, although there is no information in the return value. | ||
```rust |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you put whitespace above this line, to separate the paragraph from the code please?
src/fn/diverging.md
Outdated
} | ||
``` | ||
|
||
As opposed to all the other types, this one cannot be instantiated, because the set of all possible values this type can have is empty. Note, that it is different from the `()` type, which has exactly one possible value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you wrap this to 80 columns please?
All requested changes should be fixed now. |
I'm starting to dislike Travis. Previous checks went just fine a the last one failed without changing the code. I tried to remove the |
src/fn/diverging.md
Outdated
|
||
As opposed to this function, which will never return the control back to the caller. | ||
|
||
```rust |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can change this to rust,ignore
so this block doesn't get doc tested, since it uses an unstable feature (and travis ci uses stable rust right now)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and maybe add that #[feature(...)]
line back in so readers know this is an unstable featuree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I didn't know about the ignore
option. :-)
@msehnout Thank you! |
Fix #146