<!doctype html><htmllang=endir=ltrclass="blog-wrapper blog-post-page plugin-blog plugin-id-default"data-has-hydrated=false><metacharset=UTF-8><metaname=generatorcontent="Docusaurus v3.6.1"><titledata-rh=true>Allocations in Rust: Summary | The Old Speice Guy</title><metadata-rh=truename=viewportcontent="width=device-width,initial-scale=1.0"><metadata-rh=truename=twitter:cardcontent=summary_large_image><metadata-rh=trueproperty=og:urlcontent=https://speice.io/2019/02/summary><metadata-rh=trueproperty=og:localecontent=en><metadata-rh=truename=docusaurus_localecontent=en><metadata-rh=truename=docusaurus_tagcontent=default><metadata-rh=truename=docsearch:languagecontent=en><metadata-rh=truename=docsearch:docusaurus_tagcontent=default><metadata-rh=trueproperty=og:titlecontent="Allocations in Rust: Summary | The Old Speice Guy"><metadata-rh=truename=descriptioncontent="While there's a lot of interesting detail captured in this series, it's often helpful to have a"><metadata-rh=trueproperty=og:descriptioncontent="While there's a lot of interesting detail captured in this series, it's often helpful to have a"><metadata-rh=trueproperty=og:typecontent=article><metadata-rh=trueproperty=article:published_timecontent=2019-02-09T12:00:00.000Z><linkdata-rh=truerel=iconhref=/img/favicon.ico><linkdata-rh=truerel=canonicalhref=https://speice.io/2019/02/summary><linkdata-rh=truerel=alternatehref=https://speice.io/2019/02/summaryhreflang=en><linkdata-rh=truerel=alternatehref=https://speice.io/2019/02/summaryhreflang=x-default><scriptdata-rh=truetype=application/ld+json>{"@context":"https://schema.org","@id":"https://speice.io/2019/02/summary","@type":"BlogPosting","author":{"@type":"Person","name":"Bradlee Speice"},"dateModified":"2024-11-10T02:05:00.000Z","datePublished":"2019-02-09T12:00:00.000Z","description":"While there's a lot of interesting detail captured in this series, it's often helpful to have a","headline":"Allocations in Rust: Summary","isPartOf":{"@id":"https://speice.io/","@type":"Blog","name":"Blog"},"keywords":[],"mainEntityOfPage":"https://speice.io/2019/02/summary","name":"Allocations in Rust: Summary","url":"https://speice.io/2019/02/summary"}</script><linkrel=alternatetype=application/rss+xmlhref=/rss.xmltitle="The Old Speice Guy RSS Feed"><linkrel=alternatetype=application/atom+xmlhref=/atom.xmltitle="The Old Speice Guy Atom Feed"><linkrel=stylesheethref=/katex/katex.min.css><linkrel=stylesheethref=/assets/css/styles.16c3428d.css><scriptsrc=/assets/js/runtime~main.29a27dcf.jsdefer></script><scriptsrc=/assets/js/main.d461af80.jsdefer></script><bodyclass=navigation-with-keyboard><script>!function(){vart,e=function(){try{returnnewURLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{returnwindow.localStorage.getItem("theme")}catch(t){}}();t=null!==e?e:"light",document.documentElement.setAttribute("data-theme",t)}(),function(){try{for(var[t,e]ofnewURLSearchParams(window.location.search).entries())if(t.startsWith("docusaurus-data-")){vara=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><divid=__docusaurus><divrole=regionaria-label="Skip to main content"><aclass=skipToContent_fXgnhref=#__docusaurus_skipToContent_fallback>Skip to main content</a></div><navaria-label=Mainclass="navbar navbar--fixed-top"><divclass=navbar__inner><divclass=navbar__items><buttonaria-label="Toggle navigation bar"aria-expanded=falseclass="navbar__toggle clean-btn"type=button><svgwidth=30height=30viewBox="0 0 30 30"aria-hidden=true><pathstroke=currentColorstroke-linecap=roundstroke-miterlimit=10stroke-width=2d="M4 7h22M4 15h22M4 23h22"/></svg></button><aclass=navbar__brandhref=/><divclass=navbar__logo><imgsrc=/img/logo.svgalt="Sierpinski Gasket"class="themedComponent_mlkZ themedComponent--light_NVdE"><imgsrc=/img/logo-dark.svgalt="Sierpinski Gasket"class="themedComponent_mlkZ themedComponent--dark_xIcU"></div><bclass="navbar__titletext--
document that answers some "yes/no" questions. You may not care about what an <code>Iterator</code> looks like
in assembly, you just need to know whether it allocates an object on the heap or not. And while Rust
will prioritize the fastest behavior it can, here are the rules for each memory type:</p>
<p><strong>Global Allocation</strong>:</p>
<ul>
<li><code>const</code> is a fixed value; the compiler is allowed to copy it wherever useful.</li>
<li><code>static</code> is a fixed reference; the compiler will guarantee it is unique.</li>
</ul>
<p><strong>Stack Allocation</strong>:</p>
<ul>
<li>Everything not using a smart pointer will be allocated on the stack.</li>
<li>Structs, enums, iterators, arrays, and closures are all stack allocated.</li>
<li>Cell types (<code>RefCell</code>) behave like smart pointers, but are stack-allocated.</li>
<li>Inlining (<code>#[inline]</code>) will not affect allocation behavior for better or worse.</li>
<li>Types that are marked <code>Copy</code> are guaranteed to have their contents stack-allocated.</li>
</ul>
<p><strong>Heap Allocation</strong>:</p>
<ul>
<li>Smart pointers (<code>Box</code>, <code>Rc</code>, <code>Mutex</code>, etc.) allocate their contents in heap memory.</li>
<li>Collections (<code>HashMap</code>, <code>Vec</code>, <code>String</code>, etc.) allocate their contents in heap memory.</li>
<li>Some smart pointers in the standard library have counterparts in other crates that don't need heap
memory. If possible, use those.</li>
</ul>
<p><imgdecoding=asyncloading=lazyalt="Container Sizes in Rust"src=/assets/images/container-size-7fd54cbb2391e3e7310b0424c5f92cc1.svgwidth=960height=540class=img_ev3q></p>