<!doctype html><htmllang=endir=ltrclass="blog-wrapper blog-list-page plugin-blog plugin-id-default"data-has-hydrated=false><metacharset=UTF-8><metaname=generatorcontent="Docusaurus v3.6.1"><titledata-rh=true>Blog | 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/page/2><metadata-rh=trueproperty=og:localecontent=en><metadata-rh=truename=docusaurus_localecontent=en><metadata-rh=truename=docsearch:languagecontent=en><metadata-rh=trueproperty=og:titlecontent="Blog | The Old Speice Guy"><metadata-rh=truename=descriptioncontent=Blog><metadata-rh=trueproperty=og:descriptioncontent=Blog><metadata-rh=truename=docusaurus_tagcontent=blog_posts_list><metadata-rh=truename=docsearch:docusaurus_tagcontent=blog_posts_list><linkdata-rh=truerel=iconhref=/img/favicon.ico><linkdata-rh=truerel=canonicalhref=https://speice.io/page/2><linkdata-rh=truerel=alternatehref=https://speice.io/page/2hreflang=en><linkdata-rh=truerel=alternatehref=https://speice.io/page/2hreflang=x-default><scriptdata-rh=truetype=application/ld+json>{"@context":"https://schema.org","@id":"https://speice.io/page/2","@type":"Blog","blogPost":[{"@id":"https://speice.io/2019/02/a-heaping-helping","@type":"BlogPosting","author":{"@type":"Person","name":"BradleeSpeice"},"dateModified":"2024-11-10T02:05:00.000Z","datePublished":"2019-02-07T12:00:00.000Z","description":"Managingdynamicmemoryishard.Somelanguagesassumeuserswilldoitthemselves(C,C++),and","headline":"AllocationsinRust:Dynamicmemory","keywords":[],"mainEntityOfPage":"https://speice.io/2019/02/a-heaping-helping","name":"AllocationsinRust:Dynamicmemory","url":"https://speice.io/2019/02/a-heaping-helping"},{"@id":"https://speice.io/2019/02/stacking-up","@type":"BlogPosting","author":{"@type":"Person","name":"BradleeSpeice"},"dateModified":"2024-11-10T02:05:00.000Z","datePublished":"2019-02-06T12:00:00.000Z","description":"constandstaticareperfectlyfine,butit'srelativelyrarethatweknowatcompile-timeabout","headline":"AllocationsinRust:Fixedmemory","keywords":[],"mainEntityOfPage":"https://speice.io/2019/02/stacking-up","name":"AllocationsinRust:Fixedmemory","url":"https://speice.io/2019/02/stacking-up"},{"@id":"https://speice.io/2019/02/the-whole-world","@type":"BlogPosting","author":{"@type":"Person","name":"BradleeSpeice"},"dateModified":"2024-11-10T02:05:00.000Z","datePublished":"2019-02-05T12:00:00.000Z","description":"Thefirstmemorytypewe'lllookatisprettyspecial:whenRustcanprovethatavalueisfixed","headline":"AllocationsinRust:Globalmemory","keywords":[],"mainEntityOfPage":"https://speice.io/2019/02/the-whole-world","name":"AllocationsinRust:Globalmemory","url":"https://speice.io/2019/02/the-whole-world"},{"@id":"https://speice.io/2019/02/understanding-allocations-in-rust","@type":"BlogPosting","author":{"@type":"Person","name":"BradleeSpeice"},"dateModified":"2024-11-10T02:05:00.000Z","datePublished":"2019-02-04T12:00:00.000Z","description":"There'sanalchemyofdistillingcomplextechnicaltopicsintoarticlesandvideosthatchangethe","headline":"AllocationsinRust:Foreword","keywords":[],"mainEntityOfPage":"https://speice.io/2019/02/understanding-allocations-in-rust","name":"AllocationsinRust:Foreword","url":"https://speice.io/2019/02/understanding-allocations-in-rust"},{"@id":"https://speice.io/2018/12/allocation-safety","@type":"BlogPosting","author":{"@type":"Person","name":"BradleeSpeice"},"dateModified":"2024-11-10T02:05:00.000Z","datePublished":"2018-12-15T12:00:00.000Z","description":"Ithinkit'spartofthehumanconditiontoignoreperfectlygoodadvicewhenitcomesourway.A","headline":"QADAPT-debug_assert!forallocations","keywords":[],"mainEntityOfPage":"https://speice.io/2018/12/allocation-safety","name":"QADAPT-debug_assert!forallocations","url":"https://speice.io/2018/12/allo
some languages go to extreme lengths to protect users from themselves (Java, Python). In Rust, how
the language uses dynamic memory (also referred to as the <strong>heap</strong>) is a system called <em>ownership</em>.
And as the docs mention, ownership
<ahref=https://doc.rust-lang.org/book/ch04-00-understanding-ownership.htmltarget=_blankrel="noopener noreferrer">is Rust's most unique feature</a>.</p>
<p>The heap is used in two situations; when the compiler is unable to predict either the <em>total size of
either values or references that will be the same for the duration of our program. Put another way,
it's not often the case that either you or your compiler knows how much memory your entire program
will ever need.</p>
<p>However, there are still some optimizations the compiler can do if it knows how much memory
individual functions will need. Specifically, the compiler can make use of "stack" memory (as
opposed to "heap" memory) which can be managed far faster in both the short- and long-term.</div><footerclass="row docusaurus-mt-lg"><divclass="col text--right"><aaria-label="Read more about Allocations in Rust: Fixed memory"href=/2019/02/stacking-up><b>Read more</b></a></div></footer></article><articleclass=margin-bottom--xl><header><h2class=title_f1Hy><ahref=/2019/02/the-whole-world>Allocations in Rust: Global memory</a></h2><divclass="container_mt6G margin-vert--md"><timedatetime=2019-02-05T12:00:00.000Z>February 5, 2019</time> · <!---->8 min read</div><divclass="margin-top--md margin-bottom--sm row"><divclass="col col--12 authorCol_Hf19"><divclass="avatar margin-bottom--sm"><divclass="avatar__intro authorDetails_lV9A"><divclass=avatar__name><spanclass=authorName_yefp>Bradlee Speice</span></div><divclass=authorSocials_rSDt><ahref=https://github.com/bspeicetarget=_blankrel="noopener noreferrer"class=authorSocialLink_owbftitle=GitHub><svgviewBox="0 0 256 250"width=1emheight=1emclass="authorSocialLink_owbf githubSvg_Uu4N"style=--dark:#000;--light:#fffpreserveAspectRatio=xMidYMid><pathd="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46 6.397 1.185 8.746-2.777 8.746-6.158 0-3.052-.12-13.135-.174-23.83-35.61 7.742-43.124-15.103-43.124-15.103-5.823-14.795-14.213-18.73-14.213-18.73-11.613-7.944.876-7.78.876-7.78 12.853.902 19.621 13.19 19.621 13.19 11.417 19.568 29.945 13.911 37.249 10.64 1.149-8.272 4.466-13.92 8.127-17.116-28.431-3.236-58.318-14.212-58.318-63.258 0-13.975 5-25.394 13.188-34.358-1.329-3.224-5.71-16.242 1.24-33.874 0 0 10.749-3.44 35.21 13.121 10.21-2.836 21.16-4.258 32.038-4.307 10.878.049 21.837 1.47 32.066 4.307 24.431-16.56 35.165-13.12 35.165-13.12 6.967 17.63 2.584 30.65 1.255 33.873 8.207 8.964 13.173 20.383 13.173 34.358 0 49.163-29.944 59.988-58.447 63.157 4.591 3.972 8.682 11.762 8.682 23.704 0 17.126-.148 30.91-.148 35.126 0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002 256 57.307 198.691 0 128.001 0Zm-80.06 182.34c-.282.636-1.283.827-2.194.39-.929-.417-1.45-1.284-1.15-1.922.276-.655 1.279-.838 2.205-.399.93.418 1.46 1.293 1.139 1.931Zm6.296 5.618c-.61.566-1.804.303-2.614-.591-.837-.892-.994-2.086-.375-2.66.63-.566 1.787-.301 2.626.591.838.903 1 2.088.363 2.66Zm4.32 7.188c-.785.545-2.067.034-2.86-1.104-.784-1.138-.784-2.503.017-3.05.795-.547 2.058-.055 2.861 1.075.782 1.157.782 2.522-.019 3.08Zm7.304 8.325c-.701.774-2.196.566-3.29-.49-1.119-1.032-1.43-2.496-.726-3.27.71-.776 2.213-.558 3.315.49 1.11 1.03 1.45 2.505.701 3.27Zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033-1.448-.439-2.395-1.613-2.103-2.626.301-1.01 1.747-1.484 3.207-1.028 1.446.436 2.396 1.602 2.095 2.622Zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95-1.53.034-2.769-.82-2.786-1.86 0-1.065 1.202-1.932 2.733-1.958 1.522-.03 2.768.818 2.768 1.868Zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37-1.485.271-2.861-.365-3.05-1.386-.184-1.056.893-2.114 2.376-2.387 1.514-.263 2.868.356 3.061 1.403Z"/></svg></a></div></div></div></div></div></header><divclass=markdown><p>The first memory type we'll look at is pretty special: when Rust can prove that a <em>value</em> is fixed
for the life of a program (<code>const</code>), and when a <em>reference</em> is unique for the life of a program
(<code>static</code> as a declaration, not
<ahref=https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#the-static-lifetimetarget=_blankrel="noopener noreferrer"><code>'static</code></a> as a
lifetime), we can make use of global memory. This special section of data is embedded directly in
the program binary so that variables are ready to go once the program loads; no additional
computation is necessary.</p>
<p>Understanding the value/reference distinction is important for reasons we'll go into below, and
while the
<ahref=https://github.com/rust-lang/rfcs/blob/master/text/0246-const-vs-static.mdtarget=_blankrel="noopener noreferrer">full specification</a> for
way programmers see the tools they interact with on a regular basis. I knew what a linker was, but
there's a staggering amount of complexity in between
<ahref="https://www.youtube.com/watch?v=dOfucXtyEsU"target=_blankrel="noopener noreferrer">the OS and <code>main()</code></a>. Rust programmers use the
<ahref=https://doc.rust-lang.org/stable/std/boxed/struct.Box.htmltarget=_blankrel="noopener noreferrer"><code>Box</code></a> type all the time, but there's a
rich history of the Rust language itself wrapped up in
<ahref=https://manishearth.github.io/blog/2017/01/10/rust-tidbits-box-is-special/target=_blankrel="noopener noreferrer">how special it is</a>.</p>
<p>In a similar vein, this series attempts to look at code and understand how memory is used; the
complex choreography of operating system, compiler, and program that frees you to focus on
functionality far-flung from frivolous book-keeping. The Rust compiler relieves a great deal of the
cognitive burden associated with memory management, but we're going to step into its world for a
<ahref=https://angel.co/blog/what-startups-really-mean-by-why-should-we-hire-youtarget=_blankrel="noopener noreferrer">What Startups Really Mean By "Why Should We Hire You?"</a>.
Having been interviewed by smaller companies (though not exactly startups), the questions and
subtexts are the same. There's often a question behind the question that you're actually trying to
answer, and I wish I spotted the nuance earlier in my career.</p>