Skip to content

Commit 81013a7

Browse files
msehnoutMartin Sehnoutka
authored andcommitted
Add section about diverging functions
1 parent 446a322 commit 81013a7

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
- [Iterator::any](fn/closures/closure_examples/iter_any.md)
7171
- [Iterator::find](fn/closures/closure_examples/iter_find.md)
7272
- [Higher Order Functions](fn/hof.md)
73+
- [Diverging functions](fn/diverging.md)
7374

7475
- [Modules](mod.md)
7576
- [Visibility](mod/visibility.md)

src/fn/diverging.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Diverging functions
2+
3+
Diverging functions never return. They are marked using `!`, which is an empty type.
4+
5+
```rust
6+
fn foo() -> ! {
7+
panic!("This call never returns.");
8+
}
9+
```
10+
11+
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.
12+
13+
For example, this functions returns as usual, although there is no information in the return value.
14+
```rust
15+
fn some_fn() {
16+
()
17+
}
18+
19+
fn main() {
20+
let a: () = some_fn();
21+
println!("This functions returns and you can see this line.")
22+
}
23+
```
24+
25+
As opposed to this function, which will never return the control back to the caller.
26+
```rust
27+
#![feature(never_type)]
28+
29+
fn main() {
30+
let x: ! = panic!("This call never returns.");
31+
println!("You will never see this line!");
32+
}
33+
```
34+
35+
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:
36+
37+
```rust
38+
fn main() {
39+
fn sum_odd_numbers(up_to: u32) -> u32 {
40+
let mut acc = 0;
41+
for i in 0..up_to {
42+
// Notice that the return type of this match expression must be u32
43+
// because of the type of the "addition" variable.
44+
let addition: u32 = match i%2 == 1 {
45+
// The "i" variable is of type u32, which is perfectly fine.
46+
true => i,
47+
// On the other hand, the "continue" expression does not return
48+
// u32, but it is still fine, because it never returns and therefore
49+
// does not violate the type requirements of the match expression.
50+
false => continue,
51+
};
52+
acc += addition;
53+
}
54+
acc
55+
}
56+
println!("Sum of odd numbers up to 9 (excluding): {}", sum_odd_numbers(9));
57+
}
58+
```
59+
60+
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()`).
61+
62+
63+

src/macro/syntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Syntax

0 commit comments

Comments
 (0)