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

Of course `trait`s can also be generic. Here we define one which reimplements the `Drop` `trait` as a generic method to `drop` itself and an input.

    // Non-copyable types.
    struct Empty;
    struct Null;

    // A trait generic over `T`.
    trait DoubleDrop<T> {
        // Define a method on the caller type which takes an
        // additional single parameter `T` and does nothing with it.
        fn double_drop(self, _: T);
    }

    // Implement `DoubleDrop<T>` for any generic parameter `T` and
    // caller `U`.
    impl<T, U> DoubleDrop<T> for U {
        // This method takes ownership of both passed arguments,
        // deallocating both.
        fn double_drop(self, _: T) {}
    }

    fn main() {
        let empty = Empty;
        let null  = Null;

        // Deallocate `empty` and `null`.
        empty.double_drop(null);

        //empty;
        //null;
        // ^ TODO: Try uncommenting these lines.
    }

### Ver también

`Drop`, `struct`, and `trait`