&&str --> &str coercion
⚓ Rust 📅 2025-11-10 👤 surdeus 👁️ 2Hey there! A rust beginner here.
I've been reading a bit about typing, and came up with a code example that I thought wouldn't compile, but did:
pub struct A {
name: String
}
impl A {
fn name(&self) -> &str {
&self.name.trim() // &&str is coerced to &str
}
}
How come in this instance rust decides to dereference this here? Does this combat the usual principle that type coercion must be explicit? Is there a way I could get &&str without the borrow checker yelling at me for returning a reference to something owned by the returning function?
4 posts - 4 participants
🏷️ Rust_feed