<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Allocation on rustbites</title><link>https://www.rustbites.com/tags/allocation/</link><description>Recent content in Allocation on rustbites</description><generator>Hugo</generator><language>en</language><lastBuildDate>Wed, 10 Jun 2026 20:23:05 +0200</lastBuildDate><atom:link href="https://www.rustbites.com/tags/allocation/index.xml" rel="self" type="application/rss+xml"/><item><title>194. Reuse One Buffer with .clear() — Allocate Once, Loop Many Times</title><link>https://www.rustbites.com/posts/bite-194/</link><pubDate>Wed, 10 Jun 2026 17:00:00 +0100</pubDate><guid>https://www.rustbites.com/posts/bite-194/</guid><description>&lt;p&gt;&lt;code&gt;with_capacity&lt;/code&gt; (bite 193) buys a buffer once instead of growing it repeatedly. But if you allocate a &lt;em&gt;fresh&lt;/em&gt; &lt;code&gt;String&lt;/code&gt; or &lt;code&gt;Vec&lt;/code&gt; inside a loop, you throw that buffer away every iteration. &lt;code&gt;.clear()&lt;/code&gt; resets the length to zero while keeping the capacity — so one allocation serves the whole loop.&lt;/p&gt;</description></item><item><title>193. Vec::with_capacity — Size Up Front, Skip the Realloc Churn</title><link>https://www.rustbites.com/posts/bite-193/</link><pubDate>Wed, 10 Jun 2026 09:00:00 +0100</pubDate><guid>https://www.rustbites.com/posts/bite-193/</guid><description>&lt;p&gt;A &lt;code&gt;Vec&lt;/code&gt; you &lt;code&gt;push&lt;/code&gt; into one element at a time doesn&amp;rsquo;t grow one element at a time — it doubles, copying every existing item to a fresh allocation each time it outgrows its buffer. If you already know how many items are coming, &lt;code&gt;Vec::with_capacity&lt;/code&gt; buys the whole buffer once.&lt;/p&gt;</description></item></channel></rss>