Skip to content

Simplify and flesh out extension trait speaker notes#3167

Merged
mgeisler merged 1 commit intogoogle:mainfrom
randomPoison:tweak-extension-trait-notes
Apr 20, 2026
Merged

Simplify and flesh out extension trait speaker notes#3167
mgeisler merged 1 commit intogoogle:mainfrom
randomPoison:tweak-extension-trait-notes

Conversation

@randomPoison
Copy link
Copy Markdown
Contributor

  • Remove some redundant notes at the top that just repeat information in the other bullet points.
  • Add a note pointing out that you need a trait to use with generics and dyn.

Comment on lines -39 to -41
- Extension methods can be easier to discover than free functions. Language
servers (e.g., `rust-analyzer`) will suggest them if you type `.` after an
instance of the foreign type.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for removing this! I super dislike this argument myself: I find it very hard to "discover" mysterious methods that appear on well-known types in a nearly implicit fashion.

Free functions are much clearer: they are imported by name (barring any glob imports) and used by name. It doesn't get more clear than this when reading the code (which is what we typically optimize for, writing the code while having a full IDE environment powered up is just a small part of the life cycle).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah agreed, I'd generally suggest starting with free functions and only reaching for an extension trait if you need some functionality that a trait provides (polymorphism or method call syntax). Discoverability is still listed as a motivation in this slide, but I don't think I'm going to emphasize that much while teaching.

@mgeisler mgeisler enabled auto-merge (squash) April 20, 2026 21:45
@mgeisler mgeisler merged commit 4a9e8d0 into google:main Apr 20, 2026
37 checks passed
@randomPoison randomPoison deleted the tweak-extension-trait-notes branch April 21, 2026 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants