Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active September 14, 2021 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpivo/2aa928a8ecabc2cae117040a9836ab91 to your computer and use it in GitHub Desktop.
Save rpivo/2aa928a8ecabc2cae117040a9836ab91 to your computer and use it in GitHub Desktop.
The `impl` Keyword in Rust

The impl Keyword in Rust

Rust doesn't have classes like other object-oriented languages. The impl (implementation) keyword in Rust acts kind of like the class keyword would.

Functions can be defined inside implementations (example taken from docs.rust-lang.org):

impl Example {
    fn boo() {
        println!("boo! Example::boo() was called!");
    }
}

Functions inside implementations can be called with :: or . syntax.

In JavaScript, we tend to think of methods as exiting on classes. In Rust, methods can exist in a variety of places, including in implementations, in structs, and in enums.

Traits are kind of like interfaces in other languages. Similar to those other langugaes, an implementation can inherit its properties from traits.

Related Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment