Traits - Rust By Example
Description: Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries.

## Keyboard shortcuts

Press `←` or `→` to navigate between chapters

Press `S` or `/` to search in the book

Press `?` to show this help

Press `Esc` to hide this help

# Traits

Annotation of lifetimes in trait methods basically are similar to functions. Note that `impl` may have annotation of lifetimes too.

    // A struct with annotation of lifetimes.
    #[derive(Debug)]
    struct Borrowed<'a> {
        x: &'a i32,
    }

    // Annotate lifetimes to impl.
    impl<'a> Default for Borrowed<'a> {
        fn default() -> Self {
            Self {
                x: &10,
            }
        }
    }

    fn main() {
        let b: Borrowed = Default::default();
        println!("b is {:?}", b);
    }

### Ver también

`trait`s