# [−]Keyword fn

The keyword for defining functions.

Functions are the primary way code is executed within Rust. Function blocks, usually just called functions, can be defined in a variety of different places and be assigned many different attributes and modifiers.

Standalone functions that just sit within a module not attached to anything else are common, but most functions will end up being inside `impl` blocks, either on another type itself, or as a trait impl for that type.

    fn standalone_function() {
        // code
    }

    pub fn public_thing(argument: bool) -> String {
        // code
    }

    struct Thing {
        foo: i32,
    }

    impl Thing {
        pub fn new() -> Self {
            Self {
                foo: 42,
            }
        }
    }Run

In addition to presenting fixed types in the form of `fn name(arg: type, ..) -> return_type`, functions can also declare a list of type parameters along with trait bounds that they fall into.

    fn generic_function<T: Clone>(x: T) -> (T, T, T) {
        (x.clone(), x.clone(), x.clone())
    }

    fn generic_where<T>(x: T) -> T
        where T: std::ops::Add<Output=T> + Copy
    {
        x + x + x
    }Run

Declaring trait bounds in the angle brackets is functionally identical to using a `where` clause. It's up to the programmer to decide which works better in each situation, but `where` tends to be better when things get longer than one line.

Along with being made public via `pub`, `fn` can also have an `extern` added for use in FFI.

For more information on the various types of functions and how they're used, consult the Rust book or the Reference.

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)