2
2
3
3
You've already seen one function so far, the ` main ` function:
4
4
5
- ``` { rust}
5
+ ``` rust
6
6
fn main () {
7
7
}
8
8
```
@@ -12,22 +12,22 @@ This is the simplest possible function declaration. As we mentioned before,
12
12
this function takes no arguments, and then some curly braces to indicate the
13
13
body. Here's a function named ` foo ` :
14
14
15
- ``` { rust}
15
+ ``` rust
16
16
fn foo () {
17
17
}
18
18
```
19
19
20
20
So, what about taking arguments? Here's a function that prints a number:
21
21
22
- ``` { rust}
22
+ ``` rust
23
23
fn print_number (x : i32 ) {
24
24
println! (" x is: {}" , x );
25
25
}
26
26
```
27
27
28
28
Here's a complete program that uses ` print_number ` :
29
29
30
- ``` { rust}
30
+ ``` rust
31
31
fn main () {
32
32
print_number (5 );
33
33
}
@@ -42,7 +42,7 @@ you add a type to the argument name, after a colon.
42
42
43
43
Here's a complete program that adds two numbers together and prints them:
44
44
45
- ``` { rust}
45
+ ``` rust
46
46
fn main () {
47
47
print_sum (5 , 6 );
48
48
}
@@ -58,7 +58,7 @@ as when you declare it.
58
58
Unlike ` let ` , you _ must_ declare the types of function arguments. This does
59
59
not work:
60
60
61
- ``` {ignore}
61
+ ``` {rust, ignore}
62
62
fn print_sum(x, y) {
63
63
println!("x is: {}", x + y);
64
64
}
@@ -79,7 +79,7 @@ sweet spot between full inference and no inference.
79
79
80
80
What about returning a value? Here's a function that adds one to an integer:
81
81
82
- ``` { rust}
82
+ ``` rust
83
83
fn add_one (x : i32 ) -> i32 {
84
84
x + 1
85
85
}
@@ -90,7 +90,7 @@ Rust functions return exactly one value, and you declare the type after an
90
90
91
91
You'll note the lack of a semicolon here. If we added it in:
92
92
93
- ``` {ignore}
93
+ ``` {rust, ignore}
94
94
fn add_one(x: i32) -> i32 {
95
95
x + 1;
96
96
}
@@ -123,7 +123,7 @@ semicolon in a return position would cause a bug.
123
123
124
124
But what about early returns? Rust does have a keyword for that, ` return ` :
125
125
126
- ``` { rust}
126
+ ``` rust
127
127
fn foo (x : i32 ) -> i32 {
128
128
if x < 5 { return x ; }
129
129
@@ -134,7 +134,7 @@ fn foo(x: i32) -> i32 {
134
134
Using a ` return ` as the last line of a function works, but is considered poor
135
135
style:
136
136
137
- ``` { rust}
137
+ ``` rust
138
138
fn foo (x : i32 ) -> i32 {
139
139
if x < 5 { return x ; }
140
140
@@ -160,5 +160,34 @@ fn foo(x: i32) -> i32 {
160
160
Because ` if ` is an expression, and it's the only expression in this function,
161
161
the value will be the result of the ` if ` .
162
162
163
- There are some additional ways to define functions, but they involve features
164
- that we haven't learned about yet, so let's just leave it at that for now.
163
+ ## Diverging functions
164
+
165
+ Rust has some special syntax for 'diverging functions', which are functions that
166
+ do not return:
167
+
168
+ ```
169
+ fn diverges() -> ! {
170
+ panic!("This function never returns!");
171
+ }
172
+ ```
173
+
174
+ ` panic! ` is a macro, similar to ` println!() ` that we've already seen. Unlike
175
+ ` println!() ` , ` panic!() ` causes the current thread of execution to crash with
176
+ the given message.
177
+
178
+ Because this function will cause a crash, it will never return, and so it has
179
+ the type '` ! ` ', which is read "diverges." A diverging function can be used
180
+ as any type:
181
+
182
+ ``` should_fail
183
+ # fn diverges() -> ! {
184
+ # panic!("This function never returns!");
185
+ # }
186
+
187
+ let x: i32 = diverges();
188
+ let x: String = diverges();
189
+ ```
190
+
191
+ We don't have a good use for diverging functions yet, because they're used in
192
+ conjunction with other Rust features. But when you see ` -> ! ` later, you'll
193
+ know what it's called.
0 commit comments