{"id":11597,"date":"2025-08-16T20:02:39","date_gmt":"2025-08-16T20:02:39","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11597"},"modified":"2025-08-16T20:02:39","modified_gmt":"2025-08-16T20:02:39","slug":"closures-in-rust","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/closures-in-rust\/","title":{"rendered":"Closures in Rust"},"content":{"rendered":"<p>A &#8220;regular&#8221; closure within Rust uses the following syntax<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet name = String::from(&quot;PutridParrot&quot;);\r\nlet hello = || println!(&quot;Hello {}&quot;, name);\r\n<\/pre>\n<p>In this simple example, the <em>name<\/em> is captured within the closure, which is the function <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n|| println!(&quot;Hello {}&quot;, name);\r\n<\/pre>\n<p>The <em>name<\/em> variable remains usable after the closure, however there&#8217;s another type of closure is the <em>Moving Closure<\/em> which uses the <em>move<\/em> keyword i.e. <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet name = String::from(&quot;PutridParrot&quot;);\r\nlet hello = move || println!(&quot;Hello {}&quot;, name);\r\n<\/pre>\n<p>The difference here is the the <em>name<\/em> variable is no longer usable after the closure. Essentially the closure takes ownership of all enclosed variables.<\/p>\n<p>The main use of <em>move<\/em> closures is within threading, so the thread takes ownership of it&#8217;s data. Async blocks often require owned values. Passing values into boxed trait objects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A &#8220;regular&#8221; closure within Rust uses the following syntax let name = String::from(&quot;PutridParrot&quot;); let hello = || println!(&quot;Hello {}&quot;, name); In this simple example, the name is captured within the closure, which is the function || println!(&quot;Hello {}&quot;, name); The name variable remains usable after the closure, however there&#8217;s another type of closure is the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[191],"tags":[],"class_list":["post-11597","post","type-post","status-publish","format-standard","hentry","category-rust"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11597"}],"version-history":[{"count":1,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11597\/revisions"}],"predecessor-version":[{"id":11598,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11597\/revisions\/11598"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}