{"id":577,"date":"2023-08-14T14:14:31","date_gmt":"2023-08-14T02:14:31","guid":{"rendered":"https:\/\/timclicks.dev\/?p=577"},"modified":"2023-09-08T15:23:02","modified_gmt":"2023-09-08T03:23:02","slug":"explaining-rusts-deref-trait","status":"publish","type":"post","link":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait","title":{"rendered":"Explaining Rust&#8217;s Deref trait"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"577\" class=\"elementor elementor-577\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-2191a8ac main-content elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"2191a8ac\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-no\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-38717f3a\" data-id=\"38717f3a\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-b8ef44d elementor-widget elementor-widget-text-editor\" data-id=\"b8ef44d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>On the surface, <code>Deref<\/code> is merely the trait that is invoked when the <a href=\"https:\/\/doc.rust-lang.org\/reference\/expressions\/operator-expr.html#the-dereference-operator\">\u201cdereference operator\u201d<\/a>, the sometimes unwelcome and intimidating a unary prefixed asterisk (<code>*T<\/code>), is invoked. That is, is describes to Rust how to take a reference as an argument and return the value. It&#8217;s designed to facilitate creating smart pointers, such as reference counting (<code>Rc<\/code> and <code>Arc<\/code> are smart pointers built by the standard library) and other types that manage a chunk of data, but want to facilitate access to that data, such as <code>Vec&lt;T&gt;<\/code> and <code>String<\/code>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a3c9976 elementor-widget elementor-widget-image\" data-id=\"a3c9976\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"2560\" height=\"1440\" src=\"https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-scaled.jpg\" class=\"attachment-full size-full wp-image-664\" alt=\"Banner: Deref trait; what it is and what it does\" srcset=\"https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-scaled.jpg 2560w, https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-300x169.jpg 300w, https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-1024x576.jpg 1024w, https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-768x432.jpg 768w, https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-1536x864.jpg 1536w, https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-2048x1152.jpg 2048w\" sizes=\"(max-width: 2560px) 100vw, 2560px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3a4a2d13 elementor-widget elementor-widget-text-editor\" data-id=\"3a4a2d13\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>In some sense, though <code>Deref<\/code> provides more power than this. To start with, it&#8217;s invoked more frequently than you might expect. Rust provides &#8220;auto-deref&#8221; behavior for method calls, which means that a) there are fewer asterisks in your code made than you might expect if you&#8217;re from a language where they&#8217;re all explicit and b) until you&#8217;re proficient in Rust, it&#8217;s sometimes unclear what&#8217;s actually happening.<\/p><p>Counter-intuitively, the <code>Deref<\/code> trait requires implementers to return a reference. That is, it provides a mutable borrow. This avoids unnecessary movement and cloning, but definitely incurs some mental overhead.\u00a0<\/p><p>To make things more complicated, library authors are allowed to use Deref to do whatever they like. This includes attempting to wedge dynamic sub-typing into the language. This so-called <a href=\"https:\/\/rust-unofficial.github.io\/patterns\/anti_patterns\/deref.html\">&#8220;deref polymorphism&#8221; is a known anti-pattern<\/a>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-728bd09 elementor-widget elementor-widget-heading\" data-id=\"728bd09\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">What is \"dereferencing\"?<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-db37f87 elementor-widget elementor-widget-text-editor\" data-id=\"db37f87\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Dereferencing means accessing the data that&#8217;s referred to by a reference. Let&#8217;s work through an example, so that it&#8217;s easy to see what I mean.<\/p><p>Let&#8217;s say we have a variable <code>a<\/code>, that is holding a value 123:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7abde64 elementor-widget elementor-widget-code-highlight\" data-id=\"7abde64\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia  \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-rust \">\n\t\t\t\t<code readonly=\"true\" class=\"language-rust\">\n\t\t\t\t\t<xmp>let a = 123;<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a8fea54 elementor-widget elementor-widget-text-editor\" data-id=\"a8fea54\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>We can now take a reference, also called borrowing in Rust terminology, to <code>a<\/code> and store it as the variable <code>b<\/code>. The ampersand (<code>&amp;<\/code>) symbol is used to do that:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d11d0dc elementor-widget elementor-widget-code-highlight\" data-id=\"d11d0dc\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia  \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-rust \">\n\t\t\t\t<code readonly=\"true\" class=\"language-rust\">\n\t\t\t\t\t<xmp>let b = &a;<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fd4625a elementor-widget elementor-widget-text-editor\" data-id=\"fd4625a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Now that we have a reference, we need to dereference it to get the value 123 again. To dereference a reference, add the asterisk (<code>*<\/code> ) symbol to the left of it.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-438e641 elementor-widget elementor-widget-code-highlight\" data-id=\"438e641\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia  \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-rust \">\n\t\t\t\t<code readonly=\"true\" class=\"language-rust\">\n\t\t\t\t\t<xmp>let c = *b;<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d08e775 elementor-widget elementor-widget-text-editor\" data-id=\"d08e775\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>To use a very stretched analogy, if buried treasure is the value, the reference might be a treasure map.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-90cbb0b elementor-widget elementor-widget-image\" data-id=\"90cbb0b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t<figure class=\"wp-caption\">\n\t\t\t\t\t\t\t\t\t\t\t<a href=\"https:\/\/watabou.github.io\/perilous-shores\/?seed=766523202&#038;tags=civilized,chaotic,bay,difficult&#038;w=1600&#038;h=900\">\n\t\t\t\t\t\t\t<img decoding=\"async\" width=\"1600\" height=\"900\" src=\"https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/fantasy-map-text-overlay.png\" class=\"attachment-full size-full wp-image-619\" alt=\"A fantasy map with overlay text, &quot;Deref allows you to access the treasure from the treasure map.&quot;\" srcset=\"https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/fantasy-map-text-overlay.png 1600w, https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/fantasy-map-text-overlay-300x169.png 300w, https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/fantasy-map-text-overlay-1024x576.png 1024w, https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/fantasy-map-text-overlay-768x432.png 768w, https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/fantasy-map-text-overlay-1536x864.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/>\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t\t\t<figcaption class=\"widget-image-caption wp-caption-text\">A reference is like a treasure map. Dereferencing returns the treasure. Built with Perilous Shores (https:\/\/watabou.github.io\/perilous-shores)<\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-29a55af elementor-widget elementor-widget-heading\" data-id=\"29a55af\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Are references the same thing as borrows? <\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8eae40c elementor-widget elementor-widget-text-editor\" data-id=\"8eae40c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Almost, but not quite. In most cases, the terms borrows and references can be used interchangeably.<\/p><p>Borrowing has a slightly stronger meaning however. It&#8217;s tied to Rust&#8217;s ownership system. All borrows are references, but a reference is not necessarily a borrow.<\/p><p>Borrowing is a Rust-specific concept, whereas references are a term that&#8217;s used in other language communities.<\/p><p>To understand the difference, I recommend watching this lecture I gave entitled &#8220;Boxes, Heaps and Stacks&#8221;\u00a0 on 1.5x speed:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9016add elementor-widget-laptop__width-inherit elementor-widget elementor-widget-video\" data-id=\"9016add\" data-element_type=\"widget\" data-e-type=\"widget\" data-settings=\"{&quot;youtube_url&quot;:&quot;https:\\\/\\\/www.youtube.com\\\/watch?v=DEE1GKMbtgw&quot;,&quot;yt_privacy&quot;:&quot;yes&quot;,&quot;video_type&quot;:&quot;youtube&quot;,&quot;controls&quot;:&quot;yes&quot;}\" data-widget_type=\"video.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-wrapper elementor-open-inline\">\n\t\t\t<div class=\"elementor-video\"><\/div>\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-cf5332d elementor-widget elementor-widget-heading\" data-id=\"cf5332d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">The simple case<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6734557 elementor-widget elementor-widget-text-editor\" data-id=\"6734557\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Generally speaking, the dereference operator works by taking a reference and returning what&#8217;s being referred to (known formally as the referent).<\/p><p>In the following snippet, <code>a<\/code> and <code>c<\/code> are both bound to 123, whereas <code>b<\/code> is a reference to the copy of the number bound to <code>a<\/code>. Because of Rust&#8217;s use of <em>copy semantics<\/em> for types which implement the <code>Copy<\/code> trait, <code>a<\/code>\u00a0 and <code>c<\/code> each have their own copy of 123.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-eaef244 elementor-widget elementor-widget-code-highlight\" data-id=\"eaef244\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-rust \">\n\t\t\t\t<code readonly=\"true\" class=\"language-rust\">\n\t\t\t\t\t<xmp>fn main() {\n    let a = 123;\n    let b = &a;\n    let c = *b;\n    \n    assert_eq!(a, c);\n}\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-02bd814 elementor-widget elementor-widget-text-editor\" data-id=\"02bd814\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=3bb6103c1f535211b410176dd5f067ce\">[plaground link]<\/a><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a7ccc8b elementor-widget elementor-widget-heading\" data-id=\"a7ccc8b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Adding custom semantics<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2149b5d elementor-widget elementor-widget-text-editor\" data-id=\"2149b5d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>The <code>Deref<\/code> trait allows implementations to define their own semantics, however. There&#8217;s no requirement for the type that&#8217;s provided by dereferencing to be the type that you started with.<\/p><p>The trait&#8217;s definition requires implementers to specify an associated type <code>Target<\/code>. Let&#8217;s see how that works:<\/p><p>In the example below, the <code>Curse&lt;T&gt;<\/code> is a tuple struct that ignores the original value. During the dereference step, it returns a reference to 42, irrespective\u00a0 of whatever was provided. I think of operator overloading as a sharp knife feature. You&#8217;re allowed to do whatever you want, but you risk cutting yourself.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3b479da elementor-widget elementor-widget-code-highlight\" data-id=\"3b479da\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-rust \">\n\t\t\t\t<code readonly=\"true\" class=\"language-rust\">\n\t\t\t\t\t<xmp>use std::ops::Deref;\n\nstruct Curse<T>(T);\n\nimpl<T> Deref for Curse<T> {\n    type Target = usize;\n    \n    fn deref(&self) -> &<Self as Deref>::Target {\n        &42 \/\/ wat\n    }\n   \n}\n\nfn main() {\n    let a = 123;\n    let b = Curse(a);\n    let c = *b;\n    \n    println!(\"{a} {c}\");\n}\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d397eaf elementor-widget elementor-widget-text-editor\" data-id=\"d397eaf\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=aa605847795ee1a34bbad724e4f75ea9\">[playground link]<\/a><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-59627f1 elementor-widget elementor-widget-heading\" data-id=\"59627f1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Essential for smart pointers<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a35b874 elementor-widget elementor-widget-text-editor\" data-id=\"a35b874\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><code>Deref<\/code> enables the definition of, and perhaps more importantly, greatly eases the use of (smart) pointer types in Rust.<\/p><p>The term \u201cpointer type\u201d can be somewhat off-putting. By this, I mean any type which manages some backing data structure. Without that backing data, the type itself would be somewhat useless. This includes types that you may not think of as pointer types, such as <code>String<\/code> and <code>Vec&lt;T&gt;<\/code>.<\/p><p>Pointer types are essentially a gateway or interface to some inner type. The <code>Deref<\/code> trait is the hook that library authors can use to provide the desired functionality.<\/p><p>The term \u201csmart pointer\u201d means a pointer type with added semantics, such as reference counting. The canonical examples in the standard library are <code>std::rc::Rc<\/code> and <code>std::sync::Arc<\/code>, which both provide shared ownership via reference counting.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b6678d2 elementor-widget elementor-widget-heading\" data-id=\"b6678d2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">The Deref trait makes references easier to use<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3ed1750 elementor-widget elementor-widget-text-editor\" data-id=\"3ed1750\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Rust code uses lots of references. That&#8217;s because, when you call functions by value in Rust, you&#8217;re saying goodbye to that value. Functions that take a arguments by value\u2014that is when the value \u201cmoves\u201d into the function\u2014take on responsibility for cleaning up those values.<\/p><p>Functions also pop up more frequently than they might seem. Consider testing whether two values are the same:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-53f0584 elementor-widget elementor-widget-code-highlight\" data-id=\"53f0584\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia  \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-rust \">\n\t\t\t\t<code readonly=\"true\" class=\"language-rust\">\n\t\t\t\t\t<xmp>a == b<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ae82b72 elementor-widget elementor-widget-text-editor\" data-id=\"ae82b72\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>To compare <code>a<\/code> and <code>b<\/code>, Rust takes references to both of them. To perform the equality comparison, <code>a == b<\/code> gets converted to the compiler to <code>a.eq(&amp;b)<\/code>. During the call to <code>eq()<\/code>, <code>a<\/code> is borrowed as <code>&amp;self<\/code>.<\/p><p>Moreover, the dot operator &#8212; the one you use when you call a method &#8212; implicitly dereferences <code>self<\/code>.<\/p><p>The alternative to implicit dereferencing would be to require programmers to do it all manually. This would add a lot of clutter to the source code.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-41f5d7b elementor-widget elementor-widget-heading\" data-id=\"41f5d7b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Deref can be dangerous<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7929f03 elementor-widget elementor-widget-text-editor\" data-id=\"7929f03\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Relying too much on <code>Deref<\/code>, particularly by using it outside of its intended use case of facilitating the creation of smart pointers, can cause a lot of confusion for people using your code. To start with, error messages might suddenly seem to refer to types that are not present in a program.<\/p><p>While it&#8217;s handy that types automatically gain the ability to call functions that they dereference into, saving the irritation of manually re-implementing a lot of boilerplate when creating new (sub-)types, following that heading too far leads to deep water.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-83b8df6 elementor-widget-divider--view-line elementor-widget elementor-widget-divider\" data-id=\"83b8df6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"divider.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-divider\">\n\t\t\t<span class=\"elementor-divider-separator\">\n\t\t\t\t\t\t<\/span>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4e09645 elementor-widget elementor-widget-heading\" data-id=\"4e09645\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Acknowledgements<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e6d3df4 elementor-widget elementor-widget-text-editor\" data-id=\"e6d3df4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Thanks to <a href=\"https:\/\/predr.ag\/blog\/\">Predrag Gruevski<\/a> for your suggestions.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3dcdf5b elementor-widget elementor-widget-heading\" data-id=\"3dcdf5b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Further Reading<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-09b23a8 elementor-icon-list--layout-traditional elementor-list-item-link-full_width elementor-widget elementor-widget-icon-list\" data-id=\"09b23a8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"icon-list.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<ul class=\"elementor-icon-list-items\">\n\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item\">\n\t\t\t\t\t\t\t\t\t\t\t<a href=\"https:\/\/doc.rust-lang.org\/book\/ch04-02-references-and-borrowing.html\">\n\n\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-icon\">\n\t\t\t\t\t\t\t<i aria-hidden=\"true\" class=\"fab fa-readme\"><\/i>\t\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text\">\"References and borrowing\" chapter of The Rust Programming Language<\/span>\n\t\t\t\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item\">\n\t\t\t\t\t\t\t\t\t\t\t<a href=\"https:\/\/doc.rust-lang.org\/reference\/expressions\/operator-expr.html#the-dereference-operator\">\n\n\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-icon\">\n\t\t\t\t\t\t\t<i aria-hidden=\"true\" class=\"fab fa-readme\"><\/i>\t\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text\">\"Operator expressions\" section of the Rust Reference <\/span>\n\t\t\t\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item\">\n\t\t\t\t\t\t\t\t\t\t\t<a href=\"https:\/\/learning.accelerant.dev\/smart-pointers-in-rust\">\n\n\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-icon\">\n\t\t\t\t\t\t\t<i aria-hidden=\"true\" class=\"fas fa-book\"><\/i>\t\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text\">Accelerated Guide to Smart Pointers in Rust<\/span>\n\t\t\t\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item\">\n\t\t\t\t\t\t\t\t\t\t\t<a href=\"https:\/\/www.fpcomplete.com\/blog\/rust-asref-asderef\/\">\n\n\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-icon\">\n\t\t\t\t\t\t\t<i aria-hidden=\"true\" class=\"fab fa-readme\"><\/i>\t\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text\">Rust AsRef vs AsDeref<\/span>\n\t\t\t\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item\">\n\t\t\t\t\t\t\t\t\t\t\t<a href=\"https:\/\/rust-unofficial.github.io\/patterns\/anti_patterns\/deref.html\">\n\n\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-icon\">\n\t\t\t\t\t\t\t<i aria-hidden=\"true\" class=\"fab fa-readme\"><\/i>\t\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text\">\"Deref Polymorphism\" chapter of Rust Design Patterns<\/span>\n\t\t\t\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t<\/ul>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c56d2c1 elementor-widget elementor-widget-heading\" data-id=\"c56d2c1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">About the author<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-aaf5118 elementor-widget elementor-widget-text-editor\" data-id=\"aaf5118\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Tim McNamara, often known online as timClicks, is the author of <a href=\"https:\/\/www.manning.com\/books\/rust-in-action?a_aid=rust&amp;a_bid=0367c58f&amp;chan=timclicks-dev&amp;%3Futm_source=rust&amp;a_aid=rust\"><em>Rust in Action<\/em><\/a> and the founder of <a href=\"https:\/\/accelerant.dev\">Accelerant<\/a>. He offers Rust coaching and <a href=\"http:\/\/patreon.com\/timClicks\">mentoring to Patreon supporters<\/a> others directly via 1:1 support. Email <a href=\"mailto:tim@accelerant.dev\">tim@accelerant.dev<\/a> to learn more.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Deref is an interesting one. Depending on your perspective, or mood, it either provides a lot of frustration because of hidden control flow or a lot of delight because it adds a lot of convenience.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"episode_type":"","audio_file":"","transcript_file":"","podmotor_file_id":"","podmotor_episode_id":"","cover_image":"","cover_image_id":"","duration":"","filesize":"","filesize_raw":"","date_recorded":"","explicit":"","block":"","itunes_episode_number":"","itunes_title":"","itunes_season_number":"","itunes_episode_type":"","footnotes":""},"categories":[35],"tags":[],"series":[],"speaker":[],"class_list":["post-577","post","type-post","status-publish","format-standard","hentry","category-article"],"episode_featured_image":false,"episode_player_image":"https:\/\/timclicks.dev\/wp-content\/plugins\/seriously-simple-podcasting\/assets\/images\/no-album-art.png","download_link":"","player_link":"","audio_player":false,"episode_data":{"playerMode":"light","subscribeUrls":{"amazon":{"key":"amazon","url":"","label":"Amazon","class":"amazon","icon":"amazon.png"},"audible":{"key":"audible","url":"","label":"Audible","class":"audible","icon":"audible.png"},"apple_podcasts":{"key":"apple_podcasts","url":"","label":"Apple Podcasts","class":"apple_podcasts","icon":"apple-podcasts.png"},"overcast":{"key":"overcast","url":"","label":"Overcast","class":"overcast","icon":"overcast.png"},"pandora":{"key":"pandora","url":"","label":"Pandora","class":"pandora","icon":"pandora.png"},"pocketcasts":{"key":"pocketcasts","url":"","label":"PocketCasts","class":"pocketcasts","icon":"pocketcasts.png"},"rss":{"key":"rss","url":"","label":"RSS","class":"rss","icon":"rss.png"},"soundcloud":{"key":"soundcloud","url":"","label":"SoundCloud","class":"soundcloud","icon":"soundcloud.png"},"spotify":{"key":"spotify","url":"","label":"Spotify","class":"spotify","icon":"spotify.png"},"youtube":{"key":"youtube","url":"","label":"YouTube","class":"youtube","icon":"youtube.png"}},"rssFeedUrl":"https:\/\/timclicks.dev\/feed\/podcast\/double-click","embedCode":"<blockquote class=\"wp-embedded-content\" data-secret=\"135Dh7j6Jq\"><a href=\"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait\">Explaining Rust&#8217;s Deref trait<\/a><\/blockquote><iframe sandbox=\"allow-scripts\" security=\"restricted\" src=\"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait\/embed#?secret=135Dh7j6Jq\" width=\"500\" height=\"350\" title=\"&#8220;Explaining Rust&#8217;s Deref trait&#8221; &#8212; timClicks (Tim McNamara)\" data-secret=\"135Dh7j6Jq\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" class=\"wp-embedded-content\"><\/iframe><script>\n\/*! This file is auto-generated *\/\n!function(d,l){\"use strict\";l.querySelector&&d.addEventListener&&\"undefined\"!=typeof URL&&(d.wp=d.wp||{},d.wp.receiveEmbedMessage||(d.wp.receiveEmbedMessage=function(e){var t=e.data;if((t||t.secret||t.message||t.value)&&!\/[^a-zA-Z0-9]\/.test(t.secret)){for(var s,r,n,a=l.querySelectorAll('iframe[data-secret=\"'+t.secret+'\"]'),o=l.querySelectorAll('blockquote[data-secret=\"'+t.secret+'\"]'),c=new RegExp(\"^https?:$\",\"i\"),i=0;i<o.length;i++)o[i].style.display=\"none\";for(i=0;i<a.length;i++)s=a[i],e.source===s.contentWindow&&(s.removeAttribute(\"style\"),\"height\"===t.message?(1e3<(r=parseInt(t.value,10))?r=1e3:~~r<200&&(r=200),s.height=r):\"link\"===t.message&&(r=new URL(s.getAttribute(\"src\")),n=new URL(t.value),c.test(n.protocol))&&n.host===r.host&&l.activeElement===s&&(d.top.location.href=t.value))}},d.addEventListener(\"message\",d.wp.receiveEmbedMessage,!1),l.addEventListener(\"DOMContentLoaded\",function(){for(var e,t,s=l.querySelectorAll(\"iframe.wp-embedded-content\"),r=0;r<s.length;r++)(t=(e=s[r]).getAttribute(\"data-secret\"))||(t=Math.random().toString(36).substring(2,12),e.src+=\"#?secret=\"+t,e.setAttribute(\"data-secret\",t)),e.contentWindow.postMessage({message:\"ready\",secret:t},\"*\")},!1)))}(window,document);\n\/\/# sourceURL=https:\/\/timclicks.dev\/wp-includes\/js\/wp-embed.min.js\n<\/script>\n"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Explaining Rust&#039;s Deref trait - timClicks (Tim McNamara)<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Explaining Rust&#039;s Deref trait - timClicks (Tim McNamara)\" \/>\n<meta property=\"og:description\" content=\"Deref is an interesting one. Depending on your perspective, or mood, it either provides a lot of frustration because of hidden control flow or a lot of delight because it adds a lot of convenience.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait\" \/>\n<meta property=\"og:site_name\" content=\"timClicks (Tim McNamara)\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-14T02:14:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-08T03:23:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-scaled.jpg\" \/>\n<meta name=\"author\" content=\"Tim McNamara\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@timclicks\" \/>\n<meta name=\"twitter:site\" content=\"@timclicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tim McNamara\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait\"},\"author\":{\"name\":\"Tim McNamara\",\"@id\":\"https:\\\/\\\/timclicks.dev\\\/#\\\/schema\\\/person\\\/cfffb8c099992059ec347de32b8b5519\"},\"headline\":\"Explaining Rust&#8217;s Deref trait\",\"datePublished\":\"2023-08-14T02:14:31+00:00\",\"dateModified\":\"2023-09-08T03:23:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait\"},\"wordCount\":1081,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/timclicks.dev\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/timclicks__deref-trait__language__thumbnail-scaled.jpg\",\"articleSection\":[\"Articles\"],\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait\",\"url\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait\",\"name\":\"Explaining Rust's Deref trait - timClicks (Tim McNamara)\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/timclicks.dev\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/timclicks.dev\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/timclicks__deref-trait__language__thumbnail-scaled.jpg\",\"datePublished\":\"2023-08-14T02:14:31+00:00\",\"dateModified\":\"2023-09-08T03:23:02+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/timclicks.dev\\\/#\\\/schema\\\/person\\\/cfffb8c099992059ec347de32b8b5519\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait#primaryimage\",\"url\":\"https:\\\/\\\/timclicks.dev\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/timclicks__deref-trait__language__thumbnail-scaled.jpg\",\"contentUrl\":\"https:\\\/\\\/timclicks.dev\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/timclicks__deref-trait__language__thumbnail-scaled.jpg\",\"width\":2560,\"height\":1440,\"caption\":\"Banner: Deref trait; what it is and what it does\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/timclicks.dev\\\/article\\\/explaining-rusts-deref-trait#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/timclicks.dev\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Explaining Rust&#8217;s Deref trait\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/timclicks.dev\\\/#website\",\"url\":\"https:\\\/\\\/timclicks.dev\\\/\",\"name\":\"timClicks (Tim McNamara)\",\"description\":\"Learn the Rust programming language with Tim McNamara!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/timclicks.dev\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/timclicks.dev\\\/#\\\/schema\\\/person\\\/cfffb8c099992059ec347de32b8b5519\",\"name\":\"Tim McNamara\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8ea1fece8caaab1c1d0efbee2122e5d6a9f0dafabd5beddf30d0d3578c155c7e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8ea1fece8caaab1c1d0efbee2122e5d6a9f0dafabd5beddf30d0d3578c155c7e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8ea1fece8caaab1c1d0efbee2122e5d6a9f0dafabd5beddf30d0d3578c155c7e?s=96&d=mm&r=g\",\"caption\":\"Tim McNamara\"},\"url\":\"https:\\\/\\\/timclicks.dev\\\/author\\\/tim\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Explaining Rust's Deref trait - timClicks (Tim McNamara)","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait","og_locale":"en_GB","og_type":"article","og_title":"Explaining Rust's Deref trait - timClicks (Tim McNamara)","og_description":"Deref is an interesting one. Depending on your perspective, or mood, it either provides a lot of frustration because of hidden control flow or a lot of delight because it adds a lot of convenience.","og_url":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait","og_site_name":"timClicks (Tim McNamara)","article_published_time":"2023-08-14T02:14:31+00:00","article_modified_time":"2023-09-08T03:23:02+00:00","og_image":[{"url":"https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-scaled.jpg","type":"","width":"","height":""}],"author":"Tim McNamara","twitter_card":"summary_large_image","twitter_creator":"@timclicks","twitter_site":"@timclicks","twitter_misc":{"Written by":"Tim McNamara","Estimated reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait#article","isPartOf":{"@id":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait"},"author":{"name":"Tim McNamara","@id":"https:\/\/timclicks.dev\/#\/schema\/person\/cfffb8c099992059ec347de32b8b5519"},"headline":"Explaining Rust&#8217;s Deref trait","datePublished":"2023-08-14T02:14:31+00:00","dateModified":"2023-09-08T03:23:02+00:00","mainEntityOfPage":{"@id":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait"},"wordCount":1081,"commentCount":0,"image":{"@id":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait#primaryimage"},"thumbnailUrl":"https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-scaled.jpg","articleSection":["Articles"],"inLanguage":"en-GB","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait#respond"]}]},{"@type":"WebPage","@id":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait","url":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait","name":"Explaining Rust's Deref trait - timClicks (Tim McNamara)","isPartOf":{"@id":"https:\/\/timclicks.dev\/#website"},"primaryImageOfPage":{"@id":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait#primaryimage"},"image":{"@id":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait#primaryimage"},"thumbnailUrl":"https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-scaled.jpg","datePublished":"2023-08-14T02:14:31+00:00","dateModified":"2023-09-08T03:23:02+00:00","author":{"@id":"https:\/\/timclicks.dev\/#\/schema\/person\/cfffb8c099992059ec347de32b8b5519"},"breadcrumb":{"@id":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait#primaryimage","url":"https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-scaled.jpg","contentUrl":"https:\/\/timclicks.dev\/wp-content\/uploads\/2023\/08\/timclicks__deref-trait__language__thumbnail-scaled.jpg","width":2560,"height":1440,"caption":"Banner: Deref trait; what it is and what it does"},{"@type":"BreadcrumbList","@id":"https:\/\/timclicks.dev\/article\/explaining-rusts-deref-trait#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/timclicks.dev\/"},{"@type":"ListItem","position":2,"name":"Explaining Rust&#8217;s Deref trait"}]},{"@type":"WebSite","@id":"https:\/\/timclicks.dev\/#website","url":"https:\/\/timclicks.dev\/","name":"timClicks (Tim McNamara)","description":"Learn the Rust programming language with Tim McNamara!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/timclicks.dev\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/timclicks.dev\/#\/schema\/person\/cfffb8c099992059ec347de32b8b5519","name":"Tim McNamara","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/8ea1fece8caaab1c1d0efbee2122e5d6a9f0dafabd5beddf30d0d3578c155c7e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/8ea1fece8caaab1c1d0efbee2122e5d6a9f0dafabd5beddf30d0d3578c155c7e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8ea1fece8caaab1c1d0efbee2122e5d6a9f0dafabd5beddf30d0d3578c155c7e?s=96&d=mm&r=g","caption":"Tim McNamara"},"url":"https:\/\/timclicks.dev\/author\/tim"}]}},"_links":{"self":[{"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/posts\/577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/comments?post=577"}],"version-history":[{"count":116,"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/posts\/577\/revisions"}],"predecessor-version":[{"id":773,"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/posts\/577\/revisions\/773"}],"wp:attachment":[{"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/media?parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/categories?post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/tags?post=577"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/series?post=577"},{"taxonomy":"speaker","embeddable":true,"href":"https:\/\/timclicks.dev\/wp-json\/wp\/v2\/speaker?post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}