From 5d7470423c81fb24c053aa9177e94731b0d6179a Mon Sep 17 00:00:00 2001 From: Caleb Robson <94545082+Spartan2909@users.noreply.github.com> Date: Sat, 10 Jun 2023 12:23:00 +0100 Subject: [PATCH] Clarify how deref coercions interact with traits --- src/ch15-02-deref.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ch15-02-deref.md b/src/ch15-02-deref.md index 23c9fe8bfc..972bb9d9d2 100644 --- a/src/ch15-02-deref.md +++ b/src/ch15-02-deref.md @@ -294,6 +294,15 @@ initial immutable reference is the only immutable reference to that data, but the borrowing rules don’t guarantee that. Therefore, Rust can’t make the assumption that converting an immutable reference to a mutable reference is possible. + +### Deref Coercion and Traits + +The compiler only performs deref coercion when matching traits if the trait method is +called via the dot operator (e.g `x.into()`), and not when the method is called +using 'fully-qualified' syntax (e.g `SomeType::from(&x)`). + +For more information, see the [Nomicon][nomicon-coercions]. [impl-trait]: ch10-02-traits.html#implementing-a-trait-on-a-type [tuple-structs]: ch05-01-defining-structs.html#using-tuple-structs-without-named-fields-to-create-different-types +[nomicon-coercions]: https://doc.rust-lang.org/nomicon/coercions.html