{
  "version": "https://jsonfeed.org/version/1",
  "title": "Ian's Digital Garden",
  "home_page_url": "https://ianwwagner.com/",
  "feed_url": "https://ianwwagner.com//tag-functional-programming.json",
  "description": "",
  "items": [
    {
      "id": "https://ianwwagner.com//ownership-benefits-beyond-memory-safety.html",
      "url": "https://ianwwagner.com//ownership-benefits-beyond-memory-safety.html",
      "title": "Ownership Benefits Beyond Memory Safety",
      "content_html": "<p>Rust's ownership system is well-known for the ways it enforces memory safety guaranteees.\nFor example, you can't use some value after it's been freed.\nFurther, it also ensures that mutability is explicit,\nand it enforces some extra rules that make <em>most</em> data races impossible.\nBut the ownership system has benefits beyond this which don't get as much press.</p>\n<p>Let's look at a fairly common design pattern: the builder.\nA builder typically takes zero or few arguments to create.\nIn Rust, it's often implemented as a <code>struct</code> that implements the <code>Default</code> trait.\nThen, you progressively &quot;chain&quot; method invocations to ergonomically\nto specify how to build the thing you want.\nFor example:</p>\n<pre><code class=\"language-rust\">let client = reqwest::Client::builder()\n    .user_agent(APP_USER_AGENT)\n    .timeout(Duration::from_secs(3))\n    .build()?;\n</code></pre>\n<p>This pattern is useful because it avoids bloated constructors with dozens of arguments.\nIt also lets you encode <strong>failability</strong> into the process:\nsome combinations of arguments may be invalid.</p>\n<p>If you look at the signature for the <code>timeout</code> function,\nyou'll find it takes <code>self</code> as its first parameter and returns a value of the same type.\nThe key thing to note is that a non-reference <code>self</code> parameter\nwill &quot;consume&quot; the receiver!\nSince it takes ownership, you can't hold on to a reference to the original value!</p>\n<p>This prevents a whole class of subtle bugs.\nPython, for example, doesn't prevent you from modifying inputs,\nand it's not always clear if a function/method is supposed to return a new value,\nwhether that value has the same contents as the original reference (which is still valid!)\nor if it's completely fresh,\nand so on.</p>\n<p>A few other languages, mostly in the purely functional tradition (Haskell comes to mind)\nalso have a similar property.\nThey don't use a concept of &quot;ownership&quot; but rather remove mutability from the language.\nRust makes what I consider to be a nice compromise\nwhich retains most of the benefits while being easier to use.</p>\n<p>In summary, the borrow checker is a powerful ally,\nand you can leverage it to make truly better APIs,\nsaving hours of debugging in the future.</p>\n",
      "summary": "",
      "date_published": "2025-05-31T00:00:00-00:00",
      "image": "",
      "authors": [
        {
          "name": "Ian Wagner",
          "url": "https://fosstodon.org/@ianthetechie",
          "avatar": "media/avi.jpeg"
        }
      ],
      "tags": [
        "rust",
        "functional programming"
      ],
      "language": "en"
    }
  ]
}