3 A _function_ consists of a [block], along with a name and a set of parameters.
4 Other than a name, all these are optional. Functions are declared with the
5 keyword `fn`. Functions may declare a set of *input* [*variables*][variables]
6 as parameters, through which the caller passes arguments into the function, and
7 the *output* [*type*][type] of the value the function will return to its caller
10 [block]: expressions/block-expr.html
11 [variables]: variables.html
14 When referred to, a _function_ yields a first-class *value* of the
15 corresponding zero-sized [*function item type*], which
16 when called evaluates to a direct call to the function.
18 [*function item type*]: types.html#function-item-types
20 For example, this is a simple function:
22 fn answer_to_life_the_universe_and_everything() -> i32 {
27 As with `let` bindings, function arguments are irrefutable patterns, so any
28 pattern that is valid in a let binding is also valid as an argument:
31 fn first((value, _): (i32, i32)) -> i32 { value }
34 The block of a function is conceptually wrapped in a block that binds the
35 argument patterns and then `return`s the value of the function's block. This
36 means that the tail expression of the block, if evaluated, ends up being
37 returned to the caller. As usual, an explicit return expression within
38 the body of the function will short-cut that implicit return, if reached.
40 For example, the function above behaves as if it was written as:
43 // argument_0 is the actual first argument passed from the caller
44 let (value, _) = argument_0;
52 A _generic function_ allows one or more _parameterized types_ to appear in its
53 signature. Each type parameter must be explicitly declared in an
54 angle-bracket-enclosed and comma-separated list, following the function name.
57 // foo is generic over A and B
59 fn foo<A, B>(x: A, y: B) {
63 Inside the function signature and body, the name of the type parameter can be
64 used as a type name. [Trait](items/traits.html) bounds can be specified for type
65 parameters to allow methods with that trait to be called on values of that
66 type. This is specified using the `where` syntax:
69 # use std::fmt::Debug;
70 fn foo<T>(x: T) where T: Debug {
74 When a generic function is referenced, its type is instantiated based on the
75 context of the reference. For example, calling the `foo` function here:
80 fn foo<T>(x: &[T]) where T: Debug {
87 will instantiate type parameter `T` with `i32`.
89 The type parameters can also be explicitly supplied in a trailing [path]
90 component after the function name. This might be necessary if there is not
91 sufficient context to determine the type parameters. For example,
92 `mem::size_of::<u32>() == 4`.
98 Extern functions are part of Rust's foreign function interface, providing the
99 opposite functionality to [external blocks]. Whereas external
100 blocks allow Rust code to call foreign code, extern functions with bodies
101 defined in Rust code _can be called by foreign code_. They are defined in the
102 same way as any other Rust function, except that they have the `extern`
106 // Declares an extern fn, the ABI defaults to "C"
107 extern fn new_i32() -> i32 { 0 }
109 // Declares an extern fn with "stdcall" ABI
110 # #[cfg(target_arch = "x86_64")]
111 extern "stdcall" fn new_i32_stdcall() -> i32 { 0 }
114 Unlike normal functions, extern fns have type `extern "ABI" fn()`. This is the
115 same type as the functions declared in an extern block.
118 # extern fn new_i32() -> i32 { 0 }
119 let fptr: extern "C" fn() -> i32 = new_i32;
122 As non-Rust calling conventions do not support unwinding, unwinding past the end
123 of an extern function will cause the process to abort. In LLVM, this is
124 implemented by executing an illegal instruction.
126 [external blocks]: items/external-blocks.html