mirror of
https://github.com/bspeice/speice.io
synced 2024-11-14 22:18:10 -05:00
1 line
10 KiB
JavaScript
1 line
10 KiB
JavaScript
|
"use strict";(self.webpackChunkspeice_io=self.webpackChunkspeice_io||[]).push([["7580"],{4935:function(e,t,n){n.r(t),n.d(t,{assets:function(){return l},contentTitle:function(){return a},default:function(){return d},frontMatter:function(){return i},metadata:function(){return s},toc:function(){return c}});var s=n(6583),o=n(5893),r=n(65);let i={slug:"2019/02/understanding-allocations-in-rust",title:"Allocations in Rust: Foreword",date:new Date("2019-02-04T12:00:00.000Z"),authors:["bspeice"],tags:[]},a=void 0,l={authorsImageUrls:[void 0]},c=[];function h(e){let t={a:"a",blockquote:"blockquote",code:"code",em:"em",hr:"hr",li:"li",ol:"ol",p:"p",ul:"ul",...(0,r.a)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)(t.p,{children:["There's an alchemy of distilling complex technical topics into articles and videos that change the\nway programmers see the tools they interact with on a regular basis. I knew what a linker was, but\nthere's a staggering amount of complexity in between\n",(0,o.jsxs)(t.a,{href:"https://www.youtube.com/watch?v=dOfucXtyEsU",children:["the OS and ",(0,o.jsx)(t.code,{children:"main()"})]}),". Rust programmers use the\n",(0,o.jsx)(t.a,{href:"https://doc.rust-lang.org/stable/std/boxed/struct.Box.html",children:(0,o.jsx)(t.code,{children:"Box"})})," type all the time, but there's a\nrich history of the Rust language itself wrapped up in\n",(0,o.jsx)(t.a,{href:"https://manishearth.github.io/blog/2017/01/10/rust-tidbits-box-is-special/",children:"how special it is"}),"."]}),"\n",(0,o.jsx)(t.p,{children:"In a similar vein, this series attempts to look at code and understand how memory is used; the\ncomplex choreography of operating system, compiler, and program that frees you to focus on\nfunctionality far-flung from frivolous book-keeping. The Rust compiler relieves a great deal of the\ncognitive burden associated with memory management, but we're going to step into its world for a\nwhile."}),"\n",(0,o.jsx)(t.p,{children:"Let's learn a bit about memory in Rust."}),"\n",(0,o.jsx)(t.hr,{}),"\n",(0,o.jsxs)(t.p,{children:["Rust's three defining features of\n",(0,o.jsx)(t.a,{href:"https://www.rust-lang.org/",children:"Performance, Reliability, and Productivity"})," are all driven to a great\ndegree by the how the Rust compiler understands memory usage. Unlike managed memory languages (Java,\nPython), Rust\n",(0,o.jsx)(t.a,{href:"https://words.steveklabnik.com/borrow-checking-escape-analysis-and-the-generational-hypothesis",children:"doesn't really"}),"\ngarbage collect; instead, it uses an\n",(0,o.jsx)(t.a,{href:"https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html",children:"ownership"}),' system to reason about\nhow long objects will last in your program. In some cases, if the life of an object is fairly\ntransient, Rust can make use of a very fast region called the "stack." When that\'s not possible,\nRust uses\n',(0,o.jsx)(t.a,{href:"https://en.wikipedia.org/wiki/Memory_management#Dynamic_memory_allocation",children:"dynamic (heap) memory"}),"\nand the ownership system to ensure you can't accidentally corrupt memory. It's not as fast, but it\nis important to have available."]}),"\n",(0,o.jsx)(t.p,{children:"That said, there are specific situations in Rust where you'd never need to worry about the\nstack/heap distinction! If you:"}),"\n",(0,o.jsxs)(t.ol,{children:["\n",(0,o.jsxs)(t.li,{children:["Never use ",(0,o.jsx)(t.code,{children:"unsafe"})]}),"\n",(0,o.jsxs)(t.li,{children:["Never use ",(0,o.jsx)(t.code,{children:"#![feature(alloc)]"})," or the ",(0,o.jsxs)(t.a,{href:"https://doc.rust-lang.org/alloc/index.html",children:[(0,o.jsx)(t.code,{children:"alloc"})," crate"]})]}),"\n"]}),"\n",(0,o.jsx)(t.p,{children:"...then it's not possible for you to use dynamic memory!"}),"\n",(0,o.jsxs)(t.p,{children:["For some uses of Rust, typically embedded devices, these constraints are OK. They have very limited\nmemory, and the program binary size itself may significantly affect what's available! There's no\noperating system able to manage this\n",(0,o.jsx)(t.a,{href:"https://en.wikipedia.org/w
|