Skip to content

Instantly share code, notes, and snippets.

@Rudxain
Created May 5, 2024 07:16
Show Gist options
  • Save Rudxain/213967eb20e114234f56a3ce4f1bac5a to your computer and use it in GitHub Desktop.
Save Rudxain/213967eb20e114234f56a3ce4f1bac5a to your computer and use it in GitHub Desktop.
How is `&str.len()` defined in Rust?

How does &str have len()?

If str is unsized, then how does a ref magically have a len method? let alone an actual return value?

One could argue "unsized types cannot exist in (safe) Rust. Therefore, an str is just an &str stripped of its size data".

But that explanation doesn't satisfy me

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