2024-12-16 21:34:48 -05:00
<!doctype html> < html lang = en dir = ltr class = "blog-wrapper blog-post-page plugin-blog plugin-id-default" data-has-hydrated = false > < meta charset = UTF-8 > < meta name = generator content = "Docusaurus v3.6.1" > < title data-rh = true > Allocations in Rust: Global memory | The Old Speice Guy< / title > < meta data-rh = true name = viewport content = "width=device-width,initial-scale=1.0" > < meta data-rh = true name = twitter:card content = summary_large_image > < meta data-rh = true property = og:url content = https://speice.io/2019/02/the-whole-world > < meta data-rh = true property = og:locale content = en > < meta data-rh = true name = docusaurus_locale content = en > < meta data-rh = true name = docusaurus_tag content = default > < meta data-rh = true name = docsearch:language content = en > < meta data-rh = true name = docsearch:docusaurus_tag content = default > < meta data-rh = true property = og:title content = "Allocations in Rust: Global memory | The Old Speice Guy" > < meta data-rh = true name = description content = "The first memory type we'll look at is pretty special: when Rust can prove that a value is fixed" > < meta data-rh = true property = og:description content = "The first memory type we'll look at is pretty special: when Rust can prove that a value is fixed" > < meta data-rh = true property = og:type content = article > < meta data-rh = true property = article:published_time content = 2019-02-05T12:00:00.000Z > < link data-rh = true rel = icon href = /img/favicon.ico > < link data-rh = true rel = canonical href = https://speice.io/2019/02/the-whole-world > < link data-rh = true rel = alternate href = https://speice.io/2019/02/the-whole-world hreflang = en > < link data-rh = true rel = alternate href = https://speice.io/2019/02/the-whole-world hreflang = x-default > < script data-rh = true type = application/ld+json > { "@context" : "https://schema.org" , "@id" : "https://speice.io/2019/02/the-whole-world" , "@type" : "BlogPosting" , "author" : { "@type" : "Person" , "name" : "Bradlee Speice" } , "dateModified" : "2024-11-10T02:05:00.000Z" , "datePublished" : "2019-02-05T12:00:00.000Z" , "description" : "The first memory type we'll look at is pretty special: when Rust can prove that a value is fixed" , "headline" : "Allocations in Rust: Global memory" , "isPartOf" : { "@id" : "https://speice.io/" , "@type" : "Blog" , "name" : "Blog" } , "keywords" : [ ] , "mainEntityOfPage" : "https://speice.io/2019/02/the-whole-world" , "name" : "Allocations in Rust: Global memory" , "url" : "https://speice.io/2019/02/the-whole-world" } < / script > < link rel = alternate type = application/rss+xml href = /rss.xml title = "The Old Speice Guy RSS Feed" > < link rel = alternate type = application/atom+xml href = /atom.xml title = "The Old Speice Guy Atom Feed" > < link rel = stylesheet href = /katex/katex.min.css > < link rel = stylesheet href = /assets/css/styles.16c3428d.css > < script src = /assets/js/runtime~main.29a27dcf.js defer > < / script > < script src = /assets/js/main.d461af80.js defer > < / script > < body class = navigation-with-keyboard > < script > ! function ( ) { var t , e = function ( ) { try { return new URLSearchParams ( window . location . search ) . get ( "docusaurus-theme" ) } catch ( t ) { } } ( ) || function ( ) { try { return window . localStorage . getItem ( "theme" ) } catch ( t ) { } } ( ) ; t = null !== e ? e : "light" , document . documentElement . setAttribute ( "data-theme" , t ) } ( ) , function ( ) { try { for ( var [ t , e ] of new URLSearchParams ( window . location . search ) . entries ( ) ) if ( t . startsWith ( "docusaurus-data-" ) ) { var a = t . replace ( "docusaurus-data-" , "data-" ) ; document . documentElement . setAttribute ( a , e ) } } catch ( t ) { } } ( ) < / script > < div id = __docusaurus > < div role = region aria-label = "Skip to main content" > < a class = skipToContent_fXgn href = #__docusaurus_skipToContent_fallback > Skip to main content< / a > < / div > < nav aria-label = Main class = "navbar navbar--fixed-top" > < div class = navbar__inner > < div class = navbar__items > < button aria-label = "Toggle navigation bar" aria-expanded = false class = "navbar__toggle clean-btn" type = button > < svg width = 30 height = 30 viewBox = "0 0 30 30" aria-hidden = true > < path stroke = currentColor stroke-linecap = round stroke-miterlimit = 10 stroke-width = 2 d = "M4 7h22M4 15h22M4 23h22" / > < / svg > < / button > < a class = navbar__brand href = / > < div class = navbar__logo > < img src = /img/logo.svg alt = "Sierpinski Gasket" class = "themedComponent_mlkZ themedComponent--light_NVdE" > < img src = /img/logo-dark.svg alt = "Sierpinski Gasket" class = "th
2024-11-10 16:43:02 -05:00
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
< a href = https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#the-static-lifetime target = _blank rel = "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
< a href = https://github.com/rust-lang/rfcs/blob/master/text/0246-const-vs-static.md target = _blank rel = "noopener noreferrer" > full specification< / a > for
these two keywords is available, we'll take a hands-on approach to the topic.< / p >
< h2 class = "anchor anchorWithStickyNavbar_LWe7" id = const-values > < code > const< / code > values< a href = #const-values class = hash-link aria-label = "Direct link to const-values" title = "Direct link to const-values" > < / a > < / h2 >
< p > When a < em > value< / em > is guaranteed to be unchanging in your program (where "value" may be scalars,
< code > struct< / code > s, etc.), you can declare it < code > const< / code > . This tells the compiler that it's safe to treat the
value as never changing, and enables some interesting optimizations; not only is there no
initialization cost to creating the value (it is loaded at the same time as the executable parts of
your program), but the compiler can also copy the value around if it speeds up the code.< / p >
< p > The points we need to address when talking about < code > const< / code > are:< / p >
< ul >
< li > < code > Const< / code > values are stored in read-only memory - it's impossible to modify.< / li >
< li > Values resulting from calling a < code > const fn< / code > are materialized at compile-time.< / li >
< li > The compiler may (or may not) copy < code > const< / code > values wherever it chooses.< / li >
< / ul >
< h3 class = "anchor anchorWithStickyNavbar_LWe7" id = read-only > Read-Only< a href = #read-only class = hash-link aria-label = "Direct link to Read-Only" title = "Direct link to Read-Only" > < / a > < / h3 >
< p > The first point is a bit strange - "read-only memory."
< a href = https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants target = _blank rel = "noopener noreferrer" > The Rust book< / a >
mentions in a couple places that using < code > mut< / code > with constants is illegal, but it's also important to
demonstrate just how immutable they are. < em > Typically< / em > in Rust you can use
< a href = https://doc.rust-lang.org/book/ch15-05-interior-mutability.html target = _blank rel = "noopener noreferrer" > interior mutability< / a > to modify
things that aren't declared < code > mut< / code > .
< a href = https://doc.rust-lang.org/std/cell/struct.RefCell.html target = _blank rel = "noopener noreferrer" > < code > RefCell< / code > < / a > provides an example of this
pattern in action:< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > use< / span > < span class = "token plain" > < / span > < span class = "token namespace" > std< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token namespace" > cell< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > my_mutator< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token plain" > cell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > & < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > < < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u8< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > >< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // Even though we're given an immutable reference,< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // the `replace` method allows us to modify the inner value.< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > cell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > .< / span > < span class = "token function" style = "color:hsl(221, 87%, 60%)" > replace< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token number" style = "color:hsl(35, 99%, 36%)" > 14< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > }< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > main< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "token punct
< p > --
< a href = "https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8e4bea1a718edaff4507944e825a54b2" target = _blank rel = "noopener noreferrer" > Rust Playground< / a > < / p >
< p > When < code > const< / code > is involved though, interior mutability is impossible:< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > use< / span > < span class = "token plain" > < / span > < span class = "token namespace" > std< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token namespace" > cell< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > const< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > CELL< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > < < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u8< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > >< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > =< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token function" style = "color:hsl(221, 87%, 60%)" > new< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token number" style = "color:hsl(35, 99%, 36%)" > 25< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > my_mutator< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token plain" > cell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > & < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > < < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u8< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > >< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > cell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > .< / span > < span class = "token function" style = "color:hsl(221, 87%, 60%)" > replace< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token number" style = "color:hsl(35, 99%, 36%)" > 14< / span > < span class = "token punctuation" style = "color
< p > --
< a href = "https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=88fe98110c33c1b3a51e341f48b8ae00" target = _blank rel = "noopener noreferrer" > Rust Playground< / a > < / p >
< p > And a second example using < a href = https://doc.rust-lang.org/std/sync/struct.Once.html target = _blank rel = "noopener noreferrer" > < code > Once< / code > < / a > :< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > use< / span > < span class = "token plain" > < / span > < span class = "token namespace" > std< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token namespace" > sync< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > Once< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > const< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > SURPRISE< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > Once< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > =< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > Once< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token function" style = "color:hsl(221, 87%, 60%)" > new< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > main< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // This is how `Once` is supposed to be used< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > SURPRISE< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > .< / span > < span class = "token function" style = "color:hsl(221, 87%, 60%)" > call_once< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token closure-params closure-punctuation punctuation" style = "color:hsl(119, 34%, 47%)" > |< / span > < span class = "token closure-params closure-punctuation punctuation" style = "color:hsl(119, 34%, 47%)" > |< / span > < span class = "token plain" > < / span > < span class = "token macro property" style = "color:hsl(5, 74%, 59%)" > println!< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token string" style = "color:hsl(119, 34%, 47%)" > "Initializing..."< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token punctua
< p > --
< a href = "https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c3cc5979b5e5434eca0f9ec4a06ee0ed" target = _blank rel = "noopener noreferrer" > Rust Playground< / a > < / p >
< p > When the
< a href = https://github.com/rust-lang/rfcs/blob/26197104b7bb9a5a35db243d639aee6e46d35d75/text/0246-const-vs-static.md target = _blank rel = "noopener noreferrer" > < code > const< / code > specification< / a >
refers to < a href = http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3055.pdf target = _blank rel = "noopener noreferrer" > "rvalues"< / a > , this
behavior is what they refer to. < a href = https://github.com/rust-lang/rust-clippy target = _blank rel = "noopener noreferrer" > Clippy< / a > will treat this
as an error, but it's still something to be aware of.< / p >
< h3 class = "anchor anchorWithStickyNavbar_LWe7" id = initialization > Initialization< a href = #initialization class = hash-link aria-label = "Direct link to Initialization" title = "Direct link to Initialization" > < / a > < / h3 >
< p > The next thing to mention is that < code > const< / code > values are loaded into memory < em > as part of your program
binary< / em > . Because of this, any < code > const< / code > values declared in your program will be "realized" at
compile-time; accessing them may trigger a main-memory lookup (with a fixed address, so your CPU may
be able to prefetch the value), but that's it.< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > use< / span > < span class = "token plain" > < / span > < span class = "token namespace" > std< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token namespace" > cell< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > const< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > CELL< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > < < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > >< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > =< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token function" style = "color:hsl(221, 87%, 60%)" > new< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token number" style = "color:hsl(35, 99%, 36%)" > 24< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > pub< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > multiply< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token plain" > value< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ->< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // CELL is stored at `.L__unnamed_1`< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > value < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > *<
< p > -- < a href = https://godbolt.org/z/Th8boO target = _blank rel = "noopener noreferrer" > Compiler Explorer< / a > < / p >
< p > The compiler creates one < code > RefCell< / code > , uses it everywhere, and never needs to call the < code > RefCell::new< / code >
function.< / p >
< h3 class = "anchor anchorWithStickyNavbar_LWe7" id = copying > Copying< a href = #copying class = hash-link aria-label = "Direct link to Copying" title = "Direct link to Copying" > < / a > < / h3 >
< p > If it's helpful though, the compiler can choose to copy < code > const< / code > values.< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > const< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > FACTOR< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > =< / span > < span class = "token plain" > < / span > < span class = "token number" style = "color:hsl(35, 99%, 36%)" > 1000< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > pub< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > multiply< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token plain" > value< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ->< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // See assembly line 4 for the `mov edi, 1000` instruction< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > value < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > *< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > FACTOR< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > }< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > pub< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > multiply_twice< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token plain" > value< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "c
< p > -- < a href = https://godbolt.org/z/ZtS54X target = _blank rel = "noopener noreferrer" > Compiler Explorer< / a > < / p >
< p > In this example, the < code > FACTOR< / code > value is turned into the < code > mov edi, 1000< / code > instruction in both the
< code > multiply< / code > and < code > multiply_twice< / code > functions; the "1000" value is never "stored" anywhere, as it's
small enough to inline into the assembly instructions.< / p >
< p > Finally, getting the address of a < code > const< / code > value is possible, but not guaranteed to be unique
(because the compiler can choose to copy values). I was unable to get non-unique pointers in my
testing (even using different crates), but the specifications are clear enough: < em > don't rely on
pointers to < code > const< / code > values being consistent< / em > . To be frank, caring about locations for < code > const< / code > values
is almost certainly a code smell.< / p >
< h2 class = "anchor anchorWithStickyNavbar_LWe7" id = static-values > < code > static< / code > values< a href = #static-values class = hash-link aria-label = "Direct link to static-values" title = "Direct link to static-values" > < / a > < / h2 >
< p > Static variables are related to < code > const< / code > variables, but take a slightly different approach. When we
declare that a < em > reference< / em > is unique for the life of a program, you have a < code > static< / code > variable
(unrelated to the < code > 'static< / code > lifetime). Because of the reference/value distinction with
< code > const< / code > /< code > static< / code > , static variables behave much more like typical "global" variables.< / p >
< p > But to understand < code > static< / code > , here's what we'll look at:< / p >
< ul >
< li > < code > static< / code > variables are globally unique locations in memory.< / li >
< li > Like < code > const< / code > , < code > static< / code > variables are loaded at the same time as your program being read into
memory.< / li >
< li > All < code > static< / code > variables must implement the
< a href = https://doc.rust-lang.org/std/marker/trait.Sync.html target = _blank rel = "noopener noreferrer" > < code > Sync< / code > < / a > marker trait.< / li >
< li > Interior mutability is safe and acceptable when using < code > static< / code > variables.< / li >
< / ul >
< h3 class = "anchor anchorWithStickyNavbar_LWe7" id = memory-uniqueness > Memory Uniqueness< a href = #memory-uniqueness class = hash-link aria-label = "Direct link to Memory Uniqueness" title = "Direct link to Memory Uniqueness" > < / a > < / h3 >
< p > The single biggest difference between < code > const< / code > and < code > static< / code > is the guarantees provided about
uniqueness. Where < code > const< / code > variables may or may not be copied in code, < code > static< / code > variables are
guarantee to be unique. If we take a previous < code > const< / code > example and change it to < code > static< / code > , the
difference should be clear:< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > static< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > FACTOR< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > =< / span > < span class = "token plain" > < / span > < span class = "token number" style = "color:hsl(35, 99%, 36%)" > 1000< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > pub< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > multiply< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token plain" > value< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ->< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // The assembly to `mul dword ptr [rip + example::FACTOR]` is how FACTOR gets used< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > value < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > *< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > FACTOR< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > }< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > pub< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > multiply_twice< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token plain" > value< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "to
< p > -- < a href = https://godbolt.org/z/uxmiRQ target = _blank rel = "noopener noreferrer" > Compiler Explorer< / a > < / p >
< p > Where < a href = #copying > previously< / a > there were plenty of references to multiplying by 1000, the new
assembly refers to < code > FACTOR< / code > as a named memory location instead. No initialization work needs to be
done, but the compiler can no longer prove the value never changes during execution.< / p >
< h3 class = "anchor anchorWithStickyNavbar_LWe7" id = initialization-1 > Initialization< a href = #initialization-1 class = hash-link aria-label = "Direct link to Initialization" title = "Direct link to Initialization" > < / a > < / h3 >
< p > Next, let's talk about initialization. The simplest case is initializing static variables with
either scalar or struct notation:< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token attribute attr-name" style = "color:hsl(35, 99%, 36%)" > #[derive(Debug)]< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > struct< / span > < span class = "token plain" > < / span > < span class = "token type-definition class-name" style = "color:hsl(35, 99%, 36%)" > MyStruct< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > x< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > }< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > static< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > MY_STRUCT< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > MyStruct< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > =< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > MyStruct< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // You can even reference other statics< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // declared later< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > x< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > MY_VAL< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > }< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > static< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > MY_VAL< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< /
< p > --
< a href = "https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b538dbc46076f12db047af4f4403ee6e" target = _blank rel = "noopener noreferrer" > Rust Playground< / a > < / p >
< p > Things can get a bit weirder when using < code > const fn< / code > though. In most cases, it just works:< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token attribute attr-name" style = "color:hsl(35, 99%, 36%)" > #[derive(Debug)]< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > struct< / span > < span class = "token plain" > < / span > < span class = "token type-definition class-name" style = "color:hsl(35, 99%, 36%)" > MyStruct< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > x< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > }< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > impl< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > MyStruct< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > const< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > new< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ->< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > MyStruct< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > MyStruct< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > x< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token number" style = "color:hsl(35, 99%, 36%)" > 24< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > }< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > }< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > }< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span
< p > --
< a href = "https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8c796a6e7fc273c12115091b707b0255" target = _blank rel = "noopener noreferrer" > Rust Playground< / a > < / p >
< p > However, there's a caveat: you're currently not allowed to use < code > const fn< / code > to initialize static
variables of types that aren't marked < code > Sync< / code > . For example,
< a href = https://doc.rust-lang.org/std/cell/struct.RefCell.html#method.new target = _blank rel = "noopener noreferrer" > < code > RefCell::new()< / code > < / a > is a
< code > const fn< / code > , but because
< a href = https://doc.rust-lang.org/std/cell/struct.RefCell.html#impl-Sync target = _blank rel = "noopener noreferrer" > < code > RefCell< / code > isn't < code > Sync< / code > < / a > , you'll
get an error at compile time:< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > use< / span > < span class = "token plain" > < / span > < span class = "token namespace" > std< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token namespace" > cell< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // error[E0277]: `std::cell::RefCell< u8>` cannot be shared between threads safely< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > static< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > MY_LOCK< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > < < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u8< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > >< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > =< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token function" style = "color:hsl(221, 87%, 60%)" > new< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token number" style = "color:hsl(35, 99%, 36%)" > 0< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < br > < / span > < / code > < / pre > < div class = buttonGroup__atx > < button type = button aria-label = "Copy code to clipboard" title = Copy class = clean-btn > < span class = copyButtonIcons_eSgA aria-hidden = true > < svg viewBox = "0 0 24 24" class = copyButtonIcon_y97N > < path fill = currentColor d = "M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z" / > < / svg > < svg viewBox = "0 0 24 24" class = copyButtonSuccessIcon_LjdS > < path fill = currentColor d = M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z / > < / svg > < / span > < / button > < / div > < / div > < / div >
< p > --
< a href = "https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c76ef86e473d07117a1700e21fd45560" target = _blank rel = "noopener noreferrer" > Rust Playground< / a > < / p >
< p > It's likely that this will
< a href = https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md target = _blank rel = "noopener noreferrer" > change in the future< / a > though.< / p >
< h3 class = "anchor anchorWithStickyNavbar_LWe7" id = the-sync-marker > The < code > Sync< / code > marker< a href = #the-sync-marker class = hash-link aria-label = "Direct link to the-sync-marker" title = "Direct link to the-sync-marker" > < / a > < / h3 >
< p > Which leads well to the next point: static variable types must implement the
< a href = https://doc.rust-lang.org/std/marker/trait.Sync.html target = _blank rel = "noopener noreferrer" > < code > Sync< / code > marker< / a > . Because they're globally
unique, it must be safe for you to access static variables from any thread at any time. Most
< code > struct< / code > definitions automatically implement the < code > Sync< / code > trait because they contain only elements
which themselves implement < code > Sync< / code > (read more in the
< a href = https://doc.rust-lang.org/nomicon/send-and-sync.html target = _blank rel = "noopener noreferrer" > Nomicon< / a > ). This is why earlier examples could
get away with initializing statics, even though we never included an < code > impl Sync for MyStruct< / code > in the
code. To demonstrate this property, Rust refuses to compile our earlier example if we add a
non-< code > Sync< / code > element to the < code > struct< / code > definition:< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > use< / span > < span class = "token plain" > < / span > < span class = "token namespace" > std< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token namespace" > cell< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > struct< / span > < span class = "token plain" > < / span > < span class = "token type-definition class-name" style = "color:hsl(35, 99%, 36%)" > MyStruct< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > x< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u32< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ,< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > y< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > RefCell< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > < < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > u8< / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > >< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ,< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > }< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // error[E0277]: `std::cell::RefCell< u8>` cannot be shared between threads safely< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > static< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > MY_STRUCT< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > MyStruct< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > =< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > MyStruct< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > x< / span > < span class = "token punctuation" styl
< p > --
< a href = "https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=40074d0248f056c296b662dbbff97cfc" target = _blank rel = "noopener noreferrer" > Rust Playground< / a > < / p >
< h3 class = "anchor anchorWithStickyNavbar_LWe7" id = interior-mutability > Interior mutability< a href = #interior-mutability class = hash-link aria-label = "Direct link to Interior mutability" title = "Direct link to Interior mutability" > < / a > < / h3 >
< p > Finally, while < code > static mut< / code > variables are allowed, mutating them is an < code > unsafe< / code > operation. If we
want to stay in < code > safe< / code > Rust, we can use interior mutability to accomplish similar goals:< / p >
< div class = "language-rust codeBlockContainer_Ckt0 theme-code-block" style = "--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)" > < div class = codeBlockContent_biex > < pre tabindex = 0 class = "prism-code language-rust codeBlock_bY9V thin-scrollbar" style = "background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)" > < code class = codeBlockLines_e6Vv > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > use< / span > < span class = "token plain" > < / span > < span class = "token namespace" > std< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token namespace" > sync< / span > < span class = "token namespace punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > Once< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // This example adapted from https://doc.rust-lang.org/std/sync/struct.Once.html#method.call_once< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > static< / span > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > INIT< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > :< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > Once< / span > < span class = "token plain" > < / span > < span class = "token operator" style = "color:hsl(221, 87%, 60%)" > =< / span > < span class = "token plain" > < / span > < span class = "token class-name" style = "color:hsl(35, 99%, 36%)" > Once< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ::< / span > < span class = "token function" style = "color:hsl(221, 87%, 60%)" > new< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > ;< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" style = display:inline-block > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token keyword" style = "color:hsl(301, 63%, 40%)" > fn< / span > < span class = "token plain" > < / span > < span class = "token function-definition function" style = "color:hsl(221, 87%, 60%)" > main< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > )< / span > < span class = "token plain" > < / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > {< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // Note that while `INIT` is declared immutable, we're still allowed< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token comment" style = "color:hsl(230, 4%, 64%)" > // to mutate its interior< / span > < span class = "token plain" > < / span > < br > < / span > < span class = token-line style = "color:hsl(230, 8%, 24%)" > < span class = "token plain" > < / span > < span class = "token constant" style = "color:hsl(35, 99%, 36%)" > INIT< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > .< / span > < span class = "token function" style = "color:hsl(221, 87%, 60%)" > call_once< / span > < span class = "token punctuation" style = "color:hsl(119, 34%, 47%)" > (< / span > < span class = "token closure-params closure-punc
< p > --
< a href = "https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3ba003a981a7ed7400240caadd384d59" target = _blank rel = "noopener noreferrer" > Rust Playground< / a > < / div > < / article > < nav class = "pagination-nav docusaurus-mt-lg" aria-label = "Blog post page navigation" > < a class = "pagination-nav__link pagination-nav__link--prev" href = /2019/02/understanding-allocations-in-rust > < div class = pagination-nav__sublabel > Older post< / div > < div class = pagination-nav__label > Allocations in Rust: Foreword< / div > < / a > < a class = "pagination-nav__link pagination-nav__link--next" href = /2019/02/stacking-up > < div class = pagination-nav__sublabel > Newer post< / div > < div class = pagination-nav__label > Allocations in Rust: Fixed memory< / div > < / a > < / nav > < / main > < div class = "col col--2" > < div class = "tableOfContents_bqdL thin-scrollbar" > < ul class = "table-of-contents table-of-contents__left-border" > < li > < a href = #const-values class = "table-of-contents__link toc-highlight" > < code > const< / code > values< / a > < ul > < li > < a href = #read-only class = "table-of-contents__link toc-highlight" > Read-Only< / a > < li > < a href = #initialization class = "table-of-contents__link toc-highlight" > Initialization< / a > < li > < a href = #copying class = "table-of-contents__link toc-highlight" > Copying< / a > < / ul > < li > < a href = #static-values class = "table-of-contents__link toc-highlight" > < code > static< / code > values< / a > < ul > < li > < a href = #memory-uniqueness class = "table-of-contents__link toc-highlight" > Memory Uniqueness< / a > < li > < a href = #initialization-1 class = "table-of-contents__link toc-highlight" > Initialization< / a > < li > < a href = #the-sync-marker class = "table-of-contents__link toc-highlight" > The < code > Sync< / code > marker< / a > < li > < a href = #interior-mutability class = "table-of-contents__link toc-highlight" > Interior mutability< / a > < / ul > < / ul > < / div > < / div > < / div > < / div > < / div > < footer class = footer > < div class = "container container-fluid" > < div class = "footer__bottom text--center" > < div class = footer__copyright > Copyright © 2024 Bradlee Speice< / div > < / div > < / div > < / footer > < / div >