Swarnim Arun

@swarnimarun

Understanding `dyn` with an `impl`

Submitted Aug 3, 2023

I have been thinking of starting an understanding Rust series, consider this the start of it.

We will discuss dyn in the context of Rust from the most straightforward analogies from other languages to a few practical and involved examples of its real-world usage, including some discussion around the idea of types and traits in general in Rust’s type system.

And finally, discuss how and when to use dyn, say over impl. Including why you can’t use it in some places at all. Eg,

trait Getter: Sized {
    fn get_getter(&self) -> &'a dyn Getter { // error
        self
    }
}

And also discuss how you would write the above in Rust nightly.


Swarnim Arun
Rust Engineer @ DeepSource

Comments

{{ gettext('Login to leave a comment') }}

{{ gettext('Post a comment…') }}
{{ gettext('New comment') }}
{{ formTitle }}

{{ errorMsg }}

{{ gettext('No comments posted yet') }}

Hosted by

A community of Rust language contributors and end-users from Bangalore. We have presence on the following telegram channels https://t.me/RustIndia https://t.me/fpncr LinkedIn: https://www.linkedin.com/company/rust-india/ Twitter (not updated frequently): https://twitter.com/rustlangin more