<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sam Pagenkopf</title>
    <description>The latest articles on Forem by Sam Pagenkopf (@hertz4).</description>
    <link>https://forem.com/hertz4</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F49831%2F153f703e-fa49-4842-bcb3-0c8d61254ca4.png</url>
      <title>Forem: Sam Pagenkopf</title>
      <link>https://forem.com/hertz4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hertz4"/>
    <language>en</language>
    <item>
      <title>Is Rust Bloated?</title>
      <dc:creator>Sam Pagenkopf</dc:creator>
      <pubDate>Wed, 27 Mar 2024 18:40:10 +0000</pubDate>
      <link>https://forem.com/hertz4/is-rust-bloated-45jj</link>
      <guid>https://forem.com/hertz4/is-rust-bloated-45jj</guid>
      <description>&lt;p&gt;Hi, my name is Sam and I think a lot about language design. I have been using or at least paying attention to Rust since 2015, and have some thoughts to share.&lt;/p&gt;

&lt;p&gt;There is an idea going around that Rust is a wonderful language, if only it wasn't so bloated. This implies that it is possible to create a "holy grail" systems language that has the same guarantees as Rust, but is comparable in language complexity to say, C. Is this true?&lt;/p&gt;

&lt;p&gt;In this article, I will argue that Rust is big not mainly because of cruft or excess, but that &lt;em&gt;Rust's scope is extremely wide, therefore big by design.&lt;/em&gt; That without new ideas to optimize between speed, safety, and complexity, there will never be a language better than Rust for the same purposes.&lt;/p&gt;

&lt;h1&gt;
  
  
  Rust is a Big Abstraction
&lt;/h1&gt;

&lt;p&gt;C is much simpler than Rust. This is because C is a thin abstraction of a generalized CPU. There is a common set of operations between almost every CPU, such as math, memory access, and subroutines. C turns this into a set of constructs which, without optimization, are trivially conversible into assembly code. Because the core ideas of what a CPU should do are fairly small in scope, C is fairly small.&lt;/p&gt;

&lt;p&gt;Compare this to Rust. Though also an abstraction of a CPU, crucially, Rust doesn't just abstract basic CPU operations, but &lt;em&gt;the set of uses of these operations which conform to Rust's safety standard.&lt;/em&gt; Before optimization, a Rust program is like a high-level way of describing a safe C program (except using LLVM). Safe as in, free of clearly unsafe operations -- the guarantees chosen by Rust eliminate obvious memory errors and dangerous practices, but are not a formal verification that a program will execute safely. That is, of course, until we enter an "unsafe" block, where these guarantees no longer exist.&lt;/p&gt;

&lt;p&gt;This leads to the first reason why Rust becomes bloated: when any given functionality is humanly verifiable to be safe, but the Rust compiler is not smart enough to know that fact, the Rust team would rather write it for you in their own "unsafe" codebase than encourage you to do it. This is a good thing. Rust adds a feature rather than forcing users to rely on unsafety or hand-rolled unpredictability. The "Any" type is better than creating your own void* with type info. Using Rc&amp;lt;RefCell&amp;gt; (reference counting) for shared mutable memory is better than either trying to access stale memory, or failing to deallocate shared memory. &lt;/p&gt;

&lt;p&gt;By doing this, the burden of working safely with the underlying machine is transferred to that of using the built-in Rust concept that can achieve a given task. But is more always better? Since one of the goals of Rust is to eliminate the need for "unsafe" wherever possible, the ever-growing diversity of demands compounds with the infinite diversity of Rust-safe operations on a turing-complete machine. For example, Rust 1.77.0 stabilizes a function called array::each_mut, which transforms an array of T into an array &amp;amp;mut T -- it takes a mutable reference to each element of the array. This is something simple in an unsafe block, but now there's a method for it.&lt;/p&gt;

&lt;p&gt;Though, why, if a function is in fact safe, must it contain an "unsafe" block? The standard response is that it involves unsafe operations such as pointer math or uninitialized variables. The deeper reason is that it is much harder to add a language feature than to add yet another standard library method. It is far more costly, and in some cases impossible, to make a compiler that can validate more operations as safe.&lt;/p&gt;

&lt;p&gt;Though, there is also a danger here. Rust programmers are always looking at docs to figure out what to use, or the meaning of what has been used. Many trivial unsafe things are still added features, and it seems there's a widening gap between being a systems programmer and being a Rust programmer. Sure, at times it's easier to reach for "unsafe", but after all of the optimizations and Rusty memory tricks are put into place, do you even know what the system is doing well enough to use it correctly?&lt;/p&gt;

&lt;h1&gt;
  
  
  The Cost of Speed
&lt;/h1&gt;

&lt;p&gt;Rust has an optimizing compiler, and one of its banner features is competitive runtime speed. When it comes down to it, run speed is second to safety in Rust's design, and everything else comes later.&lt;/p&gt;

&lt;p&gt;The best way to allow for code optimization is to reduce the number of guarantees, assuming that said code is already using a fast algorithm, etc. For example, if the layout of a struct can be changed, if a pointer can be reallocated freely, or if a stack variable can be reused, this gives more freedom to the optimizer. Since Rust must optimize in order to be competitive, the number of guarantees is minimal and more explicit descriptions are required, increasing the complexity required to prove safety, and littering Rust codebases with little pieces of description syntax that feel unnecessary, and sometimes are.&lt;/p&gt;

&lt;p&gt;Sadly, inferring usage to eliminate these descriptions would both slow down compile times and cause programmers to use slower patterns without realizing. One of the causes of slow C code is that implicit struct copying is built into the language. Rust avoids this pitfall by making struct copying explicit. Though modern C compilers are likely to remove any given copy of a large struct, even relying on it in the first place changes programmer behavior and can cause less optimizable code. So in another way, Rust is fast because in many cases going slower is more effort, by design, or at the very least a programmer using slow techniques is aware that they are using them.&lt;/p&gt;

&lt;p&gt;Early Rust had the philosophy that explicit is better than implicit. This enabled them to move quickly by shifting the burden of compiler intelligence onto the programmer, which in many cases makes users smarter about what they're doing, despite the ugly code that resulted. In the last few years, the Rust team has taught the compiler to infer much more and reduced this load, as they will continue to do. Rustc has learned to infer lifetimes instead of forcing them to be declared, and added things to simplify the language such as "impl Trait". &lt;a href="https://blog.rust-lang.org/inside-rust/2023/10/06/polonius-update.html"&gt;Rust is still working on Polonius&lt;/a&gt;, a new borrow checker to reduce the amount of gymnastics needed to satisfy rustc, and it surely has even more in the oven. Though, some things are just better when they're specified.&lt;/p&gt;

&lt;h1&gt;
  
  
  How Rust Avoids Bloat
&lt;/h1&gt;

&lt;p&gt;Rust being bloated right now isn't the whole story. Rust has deprecated features &lt;a href="https://github.com/rust-lang/rfcs/blob/master/text/0230-remove-runtime.md"&gt;such as green threads&lt;/a&gt; after they proved outside of Rust's philosophy. &lt;/p&gt;

&lt;p&gt;Rust also has editions, where a new version is released not expected to work with existing code. This allows Rust to periodically revolutionize itself instead of becoming stale. For example in 2018, &lt;a href="https://doc.rust-lang.org/edition-guide/rust-2018/path-changes.html"&gt;Rust overhauled their module system&lt;/a&gt;. Though, in 2021, Rust's edition was entirely additions to the language. It is possible that Rust has lost its plasticity, even though the potential still exists for less. Also, Rust must still maintain support for these old editions, with &lt;a href="https://users.rust-lang.org/t/why-crates-for-rust-2015-are-still-around/92361"&gt;some crates still using 2015 edition&lt;/a&gt;. No feature is every truly removed, out of fear of breaking existing code. Does it now make sense why &lt;a href="https://foundation.rust-lang.org/static/publications/annual-reports/annual-report-2023.pdf"&gt;\$2.5 million per year goes to Rust&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;Also, like any good FOSS team, Rust is resistant to adding new features, requiring them to go through &lt;a href="https://rfcbot.rs/"&gt;an RFC process&lt;/a&gt; where each feature is proposed, described in detail, and may be approved. A long list of potential features has been tabled, or partially implemented behind a feature gate in nightly, only to be kicked down to a future stable if at all. Not that processes like this have kept C++ from becoming intimidating, it certainly helps and gives conscious direction to Rust's progression.&lt;/p&gt;

&lt;p&gt;Finally, for better or worse, Rust also relies on an extensive package ecosystem of packages ("crates") in order to keep the core language smaller. Though one could argue that essentials like num-traits should be part of the standard language, this at least shifts responsibility away from the standard library in covering every possible use case. Even still, this poses the issue that multiple rust crates are likely to cover the same functionality, meaning that more work goes both into selecting the right crate and towards understanding the code written using a given crate. For many programmers, Rust isn't just Rust, but Rust with num-traits, rand, Serde, static_assert, lazy_static, clap, etc.&lt;/p&gt;

&lt;h1&gt;
  
  
  Room for Improvement
&lt;/h1&gt;

&lt;p&gt;So, where is this holy grail language that is about as simple as C, but as fast and as safe as Rust? This design space needs elaboration -- is it even possible? Like any human creation, Rust does have excess complexity, though much of it is not derived from the fundamental problem space, but from the one Rust has chosen to solve. Instead of just a fast, safe systems language, Rust has decided to be much more.&lt;/p&gt;

&lt;p&gt;Much of this stems from Rust being multi-paradigm, with both imperative in-place mutable patterns on one hand, and FP immutable patterns on the other. Paradoxically, FP is easier to check as memory safe, but imperative is more similar to how a computer operates. A new language would have to choose one paradigm and stick with it, rather than building out both in-place mutable APIs and immutable FP ones. This also is related to the duplicity between memory moves, mutable references, and immutable references, which all seem to warrant their own separate built-ins.&lt;/p&gt;

&lt;p&gt;Rust also has a tendency, like C++, to duplicate functionality across similar but different concepts. Why are there both functions and closures? Why are there both generic parameters and associated types? Why do data types with "impl" and modules seem to duplicate functionality between one another, such as namespacing?&lt;/p&gt;

&lt;p&gt;Also, why does Rust have both "const fn"s as well as macros and type genericity? If a function is "const" in Rust, it is guaranteed to work at compile time. Why, then, would const fns not be able to replace the functionality of macros, or to serve as type generators? Meanwhile, work still continues on &lt;a href="https://blog.rust-lang.org/2021/02/26/const-generics-mvp-beta.html"&gt;const generics&lt;/a&gt;, which allow Rust functions to take values at compile time in the same place as type parameters. Surely this is useful for things like Arrays, which are oddly limited in Rust, but this feature adds even more overlap among compile-time concepts.&lt;/p&gt;

&lt;p&gt;Not to mention the growth in Rust's standard library, which seems like it can't stop adding methods to built-ins. Why are there so many &lt;a href="https://doc.rust-lang.org/std/iter/trait.Iterator.html"&gt;stream iterator methods?&lt;/a&gt; A dozen of these could be based on passing a simple function into another existing method, and others still would be perfectly fine as a "for" loop. Who is using &lt;a href="https://doc.rust-lang.org/std/option/enum.Option.html#method.xor"&gt;the xor method on Option&lt;/a&gt;, or the &lt;a href="https://doc.rust-lang.org/std/primitive.slice.html#"&gt;billion slice methods?&lt;/a&gt; To me, much of this screams "put it in a crate!"&lt;/p&gt;

&lt;p&gt;Though, doubtlessly the Rust team has thought over these decisions thoroughly and decided to stick with the result, not arbitrarily. Even though there is a lot of growth, the growth is largely self-consistent within the parameters Rust has chosen.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Rust has trailblazed within the design space of systems languages, and I am still going to use it because there is no other language that can do what Rust does.&lt;/p&gt;

&lt;p&gt;That said, it is also time for Rust to push against bloat, even though in some ways it is already too late. What I'm hoping to see in the next edition of Rust is not expansionism like in 2021. I'm hoping to see that Rust refines its vision and addresses the public concern about the size of the language. Rust needs to decide what is "The Rust Way", and cut out anything that goes against its core philosophy.&lt;/p&gt;

&lt;p&gt;If there is ever going to be a successor to Rust that aims closer to the "holy grail", it will likely either be not as fast, or it will rely on an even more complex compiler. It will, as any language, suffer from the results of its chosen limitations, which introduces a different set of problems than those in Rust itself. I will continue to watch this space closely, but not jumping off of the Rust ship.&lt;/p&gt;

&lt;p&gt;And for those of you who avoiding Rust because of the sheer number of concepts, or feeling intimidated by this article, let me just say one more thing. I believe all systems programmers should know enough Rust to master its base concepts. Almost any well-written piece of systems code should be able to be rewritten in safe Rust without an issue. For experienced programmers, Rust is probably the best systems language to start with, since it will teach you many good practices right off of the bat.&lt;/p&gt;

&lt;p&gt;I'm optimistic about the future of systems programming, and I feel grateful that software engineers can have access to such a great open-source ecosystem of languages and libraries, which is unprecedented compared to any other field.&lt;/p&gt;

&lt;p&gt;It is also not a surprise to me when there is friction between the magical thinking rock that is a CPU and soft fleshy humans who are just trying to make life a little easier. With that in mind, I hope language designers will realize that every single thing they add is another thing to learn. Less is more, and simplicity will win the hearts of programmers. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Oops, that Sveltekit route conflict...</title>
      <dc:creator>Sam Pagenkopf</dc:creator>
      <pubDate>Thu, 11 May 2023 06:35:32 +0000</pubDate>
      <link>https://forem.com/hertz4/oops-that-sveltekit-route-conflict-4d9b</link>
      <guid>https://forem.com/hertz4/oops-that-sveltekit-route-conflict-4d9b</guid>
      <description>&lt;p&gt;All I wanted to do was take a game I made in love2d and embed it in my Sveltekit webpage. Simple, right?&lt;/p&gt;

&lt;p&gt;Well, to get love2d working on web, I needed to use a page generator called love.js. But, the HTML pages generated by love.js contain a lot of code that I wouldn't touch with a ten-foot pole — the type of magic Javascript that makes you run away to the nearest framework. So, I did the sensible thing. I forked love.js to strip down the generated webpage to the bare minimum, moved that site into &lt;code&gt;/static/games/dig/index.html&lt;/code&gt;, and referenced it from &lt;code&gt;/src/routes/games/dig/+page.svelte&lt;/code&gt;, inside of an iframe where hopefully it would never touch my webpage. Svelte gave me a warning, saying I shouldn't include raw HTML, but if it works, it works.&lt;/p&gt;

&lt;p&gt;It did work but, something inexplicable started to happen, something that nothing seemed to fix. Going from my homepage to the Dig game, the game loaded perfectly. However, going &lt;a href="https://44100.xyz/games/dig"&gt;straight to the URL&lt;/a&gt;, the game would fail to load, and even more strangely, the webpage would have no elements on it except for the game.&lt;/p&gt;

&lt;p&gt;Turns out, &lt;a href="https://github.com/sveltejs/kit/issues/6236"&gt;this is the issue that I ran into.&lt;/a&gt; By the time I realized that &lt;em&gt;this&lt;/em&gt; was my issue, I had already solved it. Yes, since two different files were competing for the same route, Sveltekit would serve either the raw HTML from the &lt;code&gt;/static&lt;/code&gt; folder, or it would serve the correctly rendered Svelte webpage, all depending on how the user navigates to the page. To fix it, I renamed &lt;code&gt;index.html&lt;/code&gt; to &lt;code&gt;_index.html&lt;/code&gt;, and now it loads perfectly. Admittedly, I didn't even know that client-side navigation was a part of Sveltekit until I encountered this issue. Well, now I definitely know!&lt;/p&gt;

&lt;p&gt;Keep this in mind when using a web framework with this sort of magic routing — it's very easy to cause a route conflict by putting static content in the same place as your framework code. IMO &lt;code&gt;/static&lt;/code&gt; feels like a hack, it's sort of a fallback for when the framework simply hasn't yet covered the ground you're trying to cover. When Svelte didn't allow me to put vanilla JS into my project, I fell back on putting things in &lt;code&gt;/static&lt;/code&gt;. Maybe there's a better solution. Think so? Comment below.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>svelte</category>
      <category>sveltekit</category>
      <category>frontend</category>
    </item>
    <item>
      <title>An FM Synth for Web (Part 2: Audio)</title>
      <dc:creator>Sam Pagenkopf</dc:creator>
      <pubDate>Fri, 10 Mar 2023 20:14:25 +0000</pubDate>
      <link>https://forem.com/hertz4/an-fm-synth-for-web-part-2-audio-2o5a</link>
      <guid>https://forem.com/hertz4/an-fm-synth-for-web-part-2-audio-2o5a</guid>
      <description>&lt;h2&gt;
  
  
  &lt;a href="https://fazeoid.netlify.app/"&gt;Demo Fazeoid here&lt;/a&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  What is Fazeoid?
&lt;/h2&gt;

&lt;p&gt;I designed Fazeoid. &lt;a href="https://github.com/neonfuz"&gt;Neonfuz&lt;/a&gt; and I implemented it in Svelte + Typescript.&lt;/p&gt;

&lt;p&gt;Fazeoid is a fun web-based synthesizer. &lt;a href="https://dev.to/hertz4/an-fm-synth-for-web-part-1-the-website-389a"&gt;In part 1, I describe the technologies used to make Fazeoid&lt;/a&gt;. In this post, I talk about details of the synthesizer itself.&lt;/p&gt;

&lt;p&gt;Fazeoid is a combination of an FM synthesizer with multiple Additive synthesizers (and I plan on adding filters, so it could also be subtractive). It uses a modulation matrix to create any possible pattern of frequency modulation, giving the user thousands of possible sounds.&lt;/p&gt;

&lt;p&gt;The synth is integrated to give live feedback (both audio and visual), giving a high potential for creative freedom.&lt;/p&gt;

&lt;h1&gt;
  
  
  FM Synthesis
&lt;/h1&gt;

&lt;p&gt;If you've ever used a Sega Genesis or a DOS computer, you may recognize &lt;a href="https://youtu.be/JpprlLkFycI"&gt;the&lt;/a&gt; &lt;a href="https://youtu.be/Cb8bEGqQWVA"&gt;sweet&lt;/a&gt; &lt;a href="https://youtu.be/yu7_I3CnDP8"&gt;sounds&lt;/a&gt; &lt;a href="https://youtu.be/yjdVflSHxN4"&gt;of&lt;/a&gt; &lt;a href="https://youtu.be/FjHon6yg-r8"&gt;FM&lt;/a&gt; &lt;a href="https://youtu.be/32S2oJuANxo"&gt;synthesis&lt;/a&gt;. These game soundtracks are using FM synths almost entirely, except for any drums or voice samples.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/playlist?list=PL5drWs8U-49vyUEdXcynqj8TaceHGV1md"&gt;If you want to hear the wide variety that FM is capable of, the channel GSTmix has a whole playlist of mixes.&lt;/a&gt; There are also many present day artists that use FM synths in their chiptune, as a step above the bleepy sounds of the NES and others. For example &lt;a href="https://www.youtube.com/@savagedregime8176"&gt;Savaged Regime&lt;/a&gt; and the &lt;a href="https://soundcloud.com/ubiktune/sets/soundshock-3-fm-funk-nirvana"&gt;artists of Ubiktune&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The synths used in old computers are actually PM synthesis, but at first I will call all of it FM. This class of synthesis actually dates much further back, since it is inherently simple, yet yields rich and varied results. Though, the 80s and onward is when it found its most common usage.&lt;/p&gt;

&lt;p&gt;The FM synth has a distinct and harmonically rich sound that can't be compared to any other. It is not a single sound, but rather an entire class of sounds based on the special relationship between two or more oscillators.&lt;/p&gt;

&lt;p&gt;Some people learn to recreate real sounds with FM synths, while others embrace the other-worldly potential of this funny oscillator. Some musicians have used it to create beautiful sound, while others have battered the FM synth into ear-splitting, tinny game soundtracks.&lt;/p&gt;

&lt;h3&gt;
  
  
  What actually is FM?
&lt;/h3&gt;

&lt;p&gt;There are many ways to grasp FM synthesis, conceptually. Intuitively speaking, imagine that you have a constant tone. Now, imagine that you wobble the pitch of the tone up and down, kind of like a singer. Now, imagine that you increase the speed of that wobbling until the character of the tone actually changes. This is the essence of FM synthesis. The reason it is called "FM" is because the pitch or &lt;em&gt;frequency&lt;/em&gt; of the audible tone is being &lt;em&gt;modulated&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Another way to understand is that the &lt;em&gt;amplitude&lt;/em&gt; of one wave is being used to alter the &lt;em&gt;phase&lt;/em&gt; of another. Imagine the function &lt;code&gt;sine(sine(time))&lt;/code&gt;. Don't even imagine it, &lt;a href="https://www.desmos.com/calculator/armn2urf49"&gt;look at it here, and adjust the slider.&lt;/a&gt;. See the complex pattern made by the output wave? This is the characteristic of an FM synth, responsible for its complex wave.&lt;/p&gt;

&lt;p&gt;A third way to understand is that an FM carrier is much like a waveshaper. A waveshaper is an operation that changes the shape of a wave, for example rounding off the peaks of a wave, or stretching the wave out. Basically, each point on a wave is run through an operation F that transforms it into another point. To do nothing, F would be &lt;code&gt;F(x) = x&lt;/code&gt;. To invert a wave, F would be &lt;code&gt;F(x) = -x&lt;/code&gt;. In the case of an FM synth, &lt;code&gt;F(x) = sine(x)&lt;/code&gt;. So if you imagine X is a low number between 0 and PI/2, the rise of the sine wave will be very similar to a straight line, and it will not alter the input sound very much. But, when the amplitude of X exceeds PI/2, the wave will begin to wrap around, bringing the peaks of the wave back downwards towards amplitude 0. And of course, eventually it will wrap back up again, too.&lt;/p&gt;

&lt;p&gt;Now, extend this thinking beyond sine waves. FM can refer to any type of wave modulating any other type of wave, each with distinct characteristics.&lt;/p&gt;

&lt;p&gt;In the following explanations, I will use "Modulator" to refer to any wave which is used to &lt;em&gt;modulate&lt;/em&gt; another wave, called the "Carrier", a wave which &lt;em&gt;carries&lt;/em&gt; the signal of the other wave in its frequency or phase. This is similar to the carrier wave used in FM radio, where the amplitude of the radio signal translates to the frequency of the radio wave within its band. Just remember, Modulator =&amp;gt; Carrier.&lt;/p&gt;

&lt;h3&gt;
  
  
  FM vs PM synthesis
&lt;/h3&gt;

&lt;p&gt;Many people inaccurately describe PM synthesis as "FM", but they aren't the same. PM is Phase Modulation, and FM is Frequency Modulation. I initially wanted Fazeoid to be a phase modulation synthesizer, hence the name, but I accidentally did frequency modulation, and stuck with it.&lt;/p&gt;

&lt;p&gt;Mathematically speaking, phase is the integral of frequency, and frequency is the derivative of phase. The way to translate between FM and PM is to take the derivative of integral of the modulator wave. With a sine wave, this does almost nothing, changing only the phase of the wave. &lt;a href="https://www.youtube.com/watch?v=Ffka-hPzug0"&gt;Human ears are very bad at hearing constant phase differences&lt;/a&gt;, so for 2OP sine-based PM or FM oscillators, things are about the same. But the moment the modulator strays from a pure tone, things change. The difference between PM and FM is reflected in the tonal characteristics of the modulator wave. Taking the derivative of any given sound wave makes the wave sound much more harsh or tinny, and this is reflected in the "tight" or "tinny" sound of PM compared to the full-bodied sound of FM. Integrating a wave makes it more muffled, of course. Though one must be careful when integrating, since any DC bias (wherein the average amplitude of the wave strays from 0) will accumulate and cause the result to go far off the rails. This is why I center all of my waves at 0.&lt;/p&gt;

&lt;p&gt;From a computer's viewpoint, PM is an &lt;code&gt;P=A&lt;/code&gt; operation, where FM is an &lt;code&gt;P=P+A&lt;/code&gt; operation. In the first case, the amplitude of the modulator is used to &lt;strong&gt;set&lt;/strong&gt; the phase of the carrier. In the second case, it is used to &lt;strong&gt;add&lt;/strong&gt; to its phase.&lt;/p&gt;

&lt;p&gt;I believe the reason why old computers used Phase Modulation is because they could utilize look-up tables to simplify this operation in their sound chips, for example using logarithms to turn the multiply into an add. &lt;a href="https://github.com/nukeykt/Nuked-OPN2"&gt;The operation of many of these old chips is fully documented and recreated.&lt;/a&gt; Since sine and multiply are pretty trivial operations today, I went whole hog and used FM. I suspect that there is an efficient way to create FM synths as well as PM synths on an 8-bit computer chip, and maybe an acute reader can correct me and point out a vintage chip, or a potential algorithm for doing so. It is possible that the real reason for this difference is based on avoiding patent infringement, or it is based on the tinnier sound of PM being better suited for tiny computer and TV speakers.&lt;/p&gt;

&lt;h3&gt;
  
  
  FM Pitch ratio
&lt;/h3&gt;

&lt;p&gt;The full formula for a phase modulated pair is actually &lt;code&gt;sin(sin(time * pitch0) * depth + time * pitch1))&lt;/code&gt;. Each oscillator has its own pitch, which is simply the rate at which the phase is incremented in Hertz, or cycles per second. This is also a very important part of their sound.&lt;/p&gt;

&lt;p&gt;One design problem that still affects fazeoid today is that FM oscillators sound best when they have integer pitch ratios to each other, resulting in more uniform waves. Right now, the pitch knobs are set to behave as arbitrary values, which encourages users to put in sub-optimal values. I will at some point implement a new UI to facilitate this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfpf48zw93qmz3q79bp9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfpf48zw93qmz3q79bp9.png" alt="Image description" width="400" height="603"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The FM Matrix
&lt;/h3&gt;

&lt;p&gt;As you have likely observed, Fazeoid is not a two-oscillator (or 2OP) synthesizer. It has 4 oscillators right now, but it will soon be expanded to an arbitrary number of oscillators. Each oscillator can feed forward its modulation into any subsequent oscillator. This means that the first oscillator is only a modulator, the last is only a carrier, and all in-between oscillators can act both as carriers and modulators.  Don't think in nouns here, but in verbs. An oscillator is a carrier or modulator insofar as it takes in modulation (carrier) or puts out modulation (modulator).&lt;/p&gt;

&lt;p&gt;Connecting several modulators in parallel to one carrier (a horizontal line on the matrix) increases the potential of that carrier in a fairly linear way. The phase effects from every modulator is summed to create the phase effect on the carrier. &lt;/p&gt;

&lt;p&gt;Connecting FM oscillators in series (a diagonal line on the matrix) exponentially increases the complexity of the output wave. The modulation isn't summed together, but rather each oscillator transforms the signal one more time.&lt;/p&gt;

&lt;p&gt;I have considered adding self-modulation (aka feedback) and reverse modulation to Fazeoid, but I stray away from it because I'm not sure how to implement them while keeping the synth independent of sample rate. These types of modulation can have very chaotic results. In analog terms, self-modulation is like adding a tiny delay to the oscillator amplitude, and then feeding that value back into its pitch. On old sound chips, that delay would be one sample. But on a modern computer, the synth can demand an arbitrary sample rate, and in order to create a consistent sound I would have to decide on a fixed feedback delay interval, and implement an allpass filter in order to delay accurately. Even then, it may not have consistent results.&lt;/p&gt;

&lt;h1&gt;
  
  
  Additive Synthesis
&lt;/h1&gt;

&lt;p&gt;Of course, I wasn't content with a sine-only FM synthesizer. But, I thought about the problem. The synths of yore used phase modulation with look-up tables, so it was easy for them to alter how the sine table was sampled in order to create new wave forms. When I implemented these same type of waves, there was a very annoying issue: aliasing aka foldover distortion noticeable in the higher frequencies. This is the audio equivalent of crusty pixel aliasing seen in video games. It's called "foldover distortion", because in the process of sampling a wave, any frequency which is above half of the sampling frequency will "fold over" and create inverted harmonics that cause nasty artifacts. Anything up to half of the sampling frequency, according to the Nyquist theorem, can be correctly represented, and anything above it causes artifacts.&lt;/p&gt;

&lt;p&gt;This is when I came up with the idea of using additive waves to recreate the waves used in old-fashioned FM sound chips, meaning that instead of just having a simple sine function for each oscillator, I will generate many many sines for each one. Now, my initial idea was actually using &lt;a href="https://en.wikipedia.org/wiki/Frequency_modulation_synthesis#2_operators"&gt;these equations&lt;/a&gt; to recreate the spectrum of the fully modulated wave, but I can't read these formulas and I have no idea how they would be extended to a whole matrix of oscillators, and especially not a matrix with varying wave types.&lt;/p&gt;

&lt;p&gt;So to avoid this aliasing issue, my additive synth generates the sounds in a loop, but refuses to generate anything above 1/4th of the sampling frequency. Why 1/4th and not 1/2? Well, when a high frequency wave is used as modulator, it can cause the carrier to alias. I found that this was a good compromise to reduce those artifacts, but there is likely a better way I have yet to find. If you have any insight, &lt;a href="https://github.com/bismuthsoft/fazeoid/issues/85"&gt;leave a comment on this issue&lt;/a&gt;. My code only knows immediate pitch by the velocity of the phase after the sum of all modulation, and by cutting out high frequency waves adaptively it has the potential to create new artifacts -- it's probably not the best solution, but what is? Also, as an optimization, I have a limit to the number of sines generated which will roll of the pitch of higher waves. This creates a softer sound than what could be, but I like the result of the compromise.&lt;/p&gt;

&lt;p&gt;So, in order to know which sines to generate, I set about finding formulas to describe which set of sines, when added together, will recreate my desired sounds. AKA, finding the discrete fourier transforms for these waves. For the first few waves, this was easy -- I just found someone else who did it. But eventually, I wanted to create more waves, and I was having trouble. Nobody online, no Wolfram Alpha, no ChatGPT could help me out in creating the fourier transforms needed. I tried to do the math, but my math is too rusty. So, I solved the problem in code, of course. By taking the integral of &lt;code&gt;sin(pitch * x) * f(x)&lt;/code&gt;, you can determine the amplitude of a particular frequency of wave &lt;code&gt;f(x)&lt;/code&gt;. So, I generated my wave, multiplied it by sine and cosine at every potential frequency, and took a riemann sum of each. The amplitude of sine and cosine are used to create a vector in the imaginary plane, of length equal to the target amplitude, and angle equal to the target phase. I used this technique to find the amplitude and phase of the component waves for each target wave, and then I put the data into excel, did a curve fit, and used that to create my harmonic series. I intentionally chose 6 waves that have different harmonic profiles, as to make the synth as interesting as possible to use. Let me know if you want the code.&lt;/p&gt;

&lt;h1&gt;
  
  
  Future plans
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://github.com/bismuthsoft/fazeoid/issues"&gt;Overall, the future plans for Fazeoid are here.&lt;/a&gt; Comment on these issues if you have any idea what I should do next.&lt;/p&gt;

&lt;h3&gt;
  
  
  Subtractive synthesis
&lt;/h3&gt;

&lt;p&gt;Fazeoid is not yet subtractive. I probably need to crack open a DSP textbook and possibly calculus in order to really proceed how I intend to. I want to create resonant filters that can be envelope controlled, for example. The approach of cutting out waves during the additive synthesis step to emulate a filter is also possible, but I'm weary of the fact that this could multiply artifacts, rather than smooth them out.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Fazeoid has been a fun adventure in synthesis land and in web frontend land. It's a space I love to be in, full of practical math, engineering, and with immediate and fun results.&lt;/p&gt;

&lt;p&gt;But, I want to pour my effort into things that will be rewarded. If you appreciate this project, please let me know however you can. If it goes ignored and unappreciated, it will never develop any further.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>audio</category>
      <category>synthesizer</category>
      <category>opensource</category>
    </item>
    <item>
      <title>An FM Synth for Web (Part 1: The Website)</title>
      <dc:creator>Sam Pagenkopf</dc:creator>
      <pubDate>Fri, 10 Mar 2023 18:45:42 +0000</pubDate>
      <link>https://forem.com/hertz4/an-fm-synth-for-web-part-1-the-website-389a</link>
      <guid>https://forem.com/hertz4/an-fm-synth-for-web-part-1-the-website-389a</guid>
      <description>&lt;h2&gt;
  
  
  &lt;a href="https://fazeoid.netlify.app/"&gt;Demo Fazeoid here&lt;/a&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  What is Fazeoid?
&lt;/h2&gt;

&lt;p&gt;I designed Fazeoid. &lt;a href="https://github.com/neonfuz"&gt;Neonfuz&lt;/a&gt; and I implemented it in Svelte + Typescript.&lt;/p&gt;

&lt;p&gt;Fazeoid is a fun web-based synthesizer. &lt;a href="https://dev.to/hertz4/an-fm-synth-for-web-part-2-audio-2o5a"&gt;I describe the synthesizer in detail in part 2&lt;/a&gt;. In this post, I talk about the technologies that made Fazeoid possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why make Fazeoid?
&lt;/h2&gt;

&lt;p&gt;Put simply, Fazeoid was made to learn and to show off. It is really amazing what's possible on the modern web.&lt;/p&gt;

&lt;p&gt;I would classify Fazeoid under "software nobody asked for", but there is potential. I mean, why not? Writing application UI for web is already common practice, and synthesizers have UI with similar requirements.&lt;/p&gt;

&lt;p&gt;The way Fazeoid generates its sound is novel, and by creating this app I was able to test it out easily. By using the Vite bundler, code changes translated to immediate differences in sound and behavior, making for a nice workflow. If you're interested, &lt;a href="https://github.com/bismuthsoft/fazeoid"&gt;Read the source code here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also, writing the frontend and sound generation code both in typescript allows the visual and audio components of the synth to be integrated with one another, with a huge potential.&lt;/p&gt;

&lt;h2&gt;
  
  
  Development Process
&lt;/h2&gt;

&lt;p&gt;Fazeoid started out as a simple idea on paper. We got the basic technology working in the web app, just to make sure it was even possible. Then, we mapped out possible designs in Figma, and finally implemented them gradually.&lt;/p&gt;

&lt;p&gt;Fazeoid was developed as a team of two, using Git of course, and Netlify CI. Mostly, features were developed on a separate branch, and then later integrated using a Pull Request.&lt;/p&gt;

&lt;p&gt;Github has a beta feature called "Projects", which has a variable-view interface integrated with the issue tracker. The main view we used was the Kanban-style view. We also planned "sprints", where I acted mostly as the "product owner". It wasn't really what you'd expect on a pro team, but the experience was valuable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4yxvevj9ej7fl1u5eels.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4yxvevj9ej7fl1u5eels.png" alt="The Kanban used to create Fazeoid" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, with help from Netlify, we were able to quickly deploy the app, as well as get previews of our features branches that are publicly available, so that things can be easily tested.&lt;/p&gt;

&lt;h2&gt;
  
  
  Svelte
&lt;/h2&gt;

&lt;p&gt;We chose Svelte for this project because of the tight integration of state and UI required, which Svelte excels at through its magical bindings. Placing code within HTML, rather than HTML within code, makes it feel a bit like writing an enhanced version of Vanilla Javascript. After having written quite a bit of React, Svelte felt very fresh, like breaking out of old chains. That didn't last, though...&lt;/p&gt;

&lt;p&gt;Svelte made it very easy to get going, but we often times questioned whether React or Solid would be a better choice. Svelte errors are often inexplicable, sometimes even dumping code even into the UI itself. When Svelte scales, it often becomes ambiguous where to store the state, or where to track it. Svelte also doesn't allow creating multiple components within the same file, which was annoying at times.&lt;/p&gt;

&lt;p&gt;Being a single static page, Svelte didn't limit us at all in terms of functionality, and we didn't have to use Sveltekit. Overall, I would use Svelte again for a project at small scale, but I would not use it to create a large website.&lt;/p&gt;

&lt;h2&gt;
  
  
  The DJ knob
&lt;/h2&gt;

&lt;p&gt;Fazeoid has an ergonomic interface using a &lt;a href="https://svelte-dj-knob.netlify.app/"&gt;custom knob element&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Svelte-dj-knob is a custom component that captures the cursor to make it easy to tweak parameters just by clicking and dragging. This was an essential part of Fazeoid. It also works on touchscreens, and is super responsive. The visual design of the knobs is flexible, but for Fazeoid I designed the knob in Inkscape, then imported the SVG into Svelte, simplified the SVG, and added animation to it using transforms. &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4grihjx6km2gi81ztzpz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4grihjx6km2gi81ztzpz.png" alt="A DJ Knob" width="158" height="145"&gt;&lt;/a&gt; If you want to use this knob in your own project, feel free to use the npm package svelte-dj-knob.&lt;/p&gt;

&lt;p&gt;In the case that Svelte becomes a real way to build synthesizer or effect UIs, this and other elements would be the starting point for a whole toolkit of elements.&lt;/p&gt;

&lt;p&gt;And why not use this element on a mundane website? I think that the web should be fun, not boring, and that user experiences should feel more like video games and less like filling out paperwork.&lt;/p&gt;

&lt;h2&gt;
  
  
  AudioWorklet
&lt;/h2&gt;

&lt;p&gt;Fazeoid would not be possible without AudioWorklet. Before AudioWorklet, software synths in javascript were severely limited. Now, the possibilities are endless.&lt;/p&gt;

&lt;p&gt;AudioWorklet is an awesome new web API that enables web applications to generate their own sound, and this has fueled a lot of cool homegrown audio projects on the web. The sound synthesis for Fazeoid is written entirely in Typescript, using the AudioWorklet more as a self-contained entity. Though the AudioWorklet API has a lot of abstractions such as &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode"&gt;BiquadFilterNode&lt;/a&gt; which could be used to extend this synthesizer, I kept its functionality contained for the sake of portability.&lt;/p&gt;

&lt;p&gt;It is definitely a bit odd to write sound code in Typescript, but it works great. The DAW Reaper already allows plugins to be written in JS, with the advantage of being able to interpret the code on the fly -- albeit with a performance hit compared to C and others. Though, as I have found, Fazeoid's performance is good enough for my phone and my PC, and that's without much optimization. If you learn how Fazeoid works, this is especially surprising.&lt;/p&gt;

&lt;p&gt;Configuring Vite to build this worklet separately and integrate it into the project was a bit of a headache, but we arrived at a simple solution. Neonfuz will eventually write a blog post explaining the process.&lt;/p&gt;

&lt;p&gt;One odd aspect of this worklet is that it is truly running on a second thread compared to the main program, and has to communicate using messages. Javascript may be single-threaded, but AudioWorklet is running more than one Javascript, and they can't just freely interoperate. Sadly, these messages can't be passed in a type-checked way, as far as I know.&lt;/p&gt;

&lt;h2&gt;
  
  
  Oscilloscopes
&lt;/h2&gt;

&lt;p&gt;Because of the fact that the audio worklet and frontend code are in the same language and source file, it was easy to make wave views by calling functions within the worklet synth. This gives the user satisfying visual feedback when turning knobs, in addition to the immediate audio feedback even on currently playing notes.&lt;/p&gt;

&lt;p&gt;The oscilloscopes are not canvases. They were made by generating SVG paths on the fly, with smooth, scalable, and responsive results.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Despite the headaches, I love the modern web. I love what it is becoming. It's a playground full of potential. Not only can I create a fully featured synthesizer, but I can put it in a website that works on any random device, even someone's phone, without installing anything. It will even be possible to add MIDI to this synth, making it a more complete project -- though I think I would prefer to port it instead so that it can be used on conventional platforms by actual musicians.&lt;/p&gt;

&lt;p&gt;I love Fazeoid, and I plan on extending it based on my time and on people's interest in the project. I hope that you love it too.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>music</category>
      <category>svelte</category>
    </item>
    <item>
      <title>Rust impl Trait For Programmers</title>
      <dc:creator>Sam Pagenkopf</dc:creator>
      <pubDate>Tue, 22 Jan 2019 01:22:27 +0000</pubDate>
      <link>https://forem.com/hertz4/rust-impl-trait-for-programmers-k15</link>
      <guid>https://forem.com/hertz4/rust-impl-trait-for-programmers-k15</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F65m72cdmjuuor8r8rzqy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F65m72cdmjuuor8r8rzqy.png" alt="Header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt; Introduction
&lt;/li&gt;
&lt;li&gt; Examples

&lt;ol&gt;
&lt;li&gt; Return impl Trait
&lt;/li&gt;
&lt;li&gt; Argument impl Trait
&lt;/li&gt;
&lt;li&gt; Relationship to dyn/dynamic types
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt; Commentary

&lt;ol&gt;
&lt;li&gt; Argument position controversy
&lt;/li&gt;
&lt;li&gt; Future of impl Trait
&lt;/li&gt;
&lt;li&gt; Where's the type theory?
&lt;/li&gt;
&lt;li&gt; Applied example (return position)
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;&lt;a id="orgf424d38"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Rust's &lt;code&gt;impl Trait&lt;/code&gt; is a little useful.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;impl Trait&lt;/code&gt; gives the ability to hide types in functions. It is best in &lt;code&gt;-&amp;gt; return&lt;/code&gt; position, but is also usable in &lt;code&gt;(argument)&lt;/code&gt; position.&lt;/p&gt;

&lt;p&gt;Use it when you know what Trait you want something for, but you don't care what it really is.&lt;/p&gt;

&lt;p&gt;&lt;a id="org450011c"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Examples
&lt;/h1&gt;

&lt;p&gt;&lt;a id="orgd8294b0"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Return impl Trait
&lt;/h2&gt;

&lt;p&gt;Here it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// First case: compiler wants more&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Display&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;does_not_work&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Whoops!"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// Second case: compiler satisfied&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;actually_works&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Display&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Not whoops!"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without impl Trait, it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;old_explicit_way_that_works&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Duh"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;slow_way_that_works&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="n"&gt;Display&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes, the old way requires you to write a big, awful type that has nothing to do with how it is actually used. This solves that.&lt;/p&gt;

&lt;p&gt;&lt;a id="org3916b39"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Argument impl Trait
&lt;/h2&gt;

&lt;p&gt;The following two things are almost identical:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;visible_type_parameter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Trait&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;hidden_type_parameter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is sometimes helpful to specify T using &lt;code&gt;turbofish_aka_explicit_type::&amp;lt;DesiredType&amp;gt;&lt;/code&gt;. This is not currntly possible with &lt;code&gt;impl T&lt;/code&gt;. Don't overthink this one; I favor type parameters. See commentary for a debate.&lt;/p&gt;

&lt;p&gt;&lt;a id="org361baaa"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Relationship to dyn/dynamic types
&lt;/h2&gt;

&lt;p&gt;Runtime Trait objects specified by &lt;code&gt;&amp;amp;'lifetime dyn Trait&lt;/code&gt; or &lt;code&gt;Box&amp;lt;dyn Trait&amp;gt;&lt;/code&gt; can be converted to compile time types. This is a conversion from dynamic to static typing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Trait&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Trait&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;dynamic_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="n"&gt;Trait&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;static_explicit_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;static_implicit_hidden_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Trait&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;static_implicit_visible_type&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Trait&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;dynamic_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;static_explicit_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;static_implicit_hidden_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//    static_implicit_hidden_type::&amp;lt;Type&amp;gt;(&amp;amp;x); doesn't work (yet?)&lt;/span&gt;
        &lt;span class="nf"&gt;static_implicit_visible_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nn"&gt;static_implicit_visible_type&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This only works when &lt;code&gt;&amp;amp;dyn Trait&lt;/code&gt; refers to a single type; i.e. you can only return one type from one function.&lt;/p&gt;

&lt;p&gt;&lt;a id="orgcfa2305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Commentary
&lt;/h1&gt;

&lt;p&gt;&lt;a id="org3776596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Argument position controversy
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/rust-lang/rfcs/pull/2444" rel="noopener noreferrer"&gt;https://github.com/rust-lang/rfcs/pull/2444&lt;/a&gt;&lt;br&gt;
I can't summarize this issue easily. Read for yourself, especially the last post, to get a sense.   &lt;/p&gt;

&lt;p&gt;&lt;a id="org1445a82"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Future of impl Trait
&lt;/h2&gt;

&lt;p&gt;Official tracking issue:&lt;br&gt;
&lt;a href="https://github.com/rust-lang/rust/issues/34511" rel="noopener noreferrer"&gt;https://github.com/rust-lang/rust/issues/34511&lt;/a&gt;&lt;br&gt;
It's January 2019. Here's what I see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Existential types (aka &lt;code&gt;type x = impl Trait&lt;/code&gt; but in a way that makes sense)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;impl X + Y&lt;/code&gt; for multiple traits.&lt;/li&gt;
&lt;li&gt;  Various other things to make it more complete.&lt;/li&gt;
&lt;li&gt;  Potential that &lt;code&gt;::&amp;lt;Turbofish&amp;gt;&lt;/code&gt; will appear a la "synthetic" type parameters. &lt;a href="https://github.com/rust-lang/rust/issues/44721#issuecomment-330945507" rel="noopener noreferrer"&gt;https://github.com/rust-lang/rust/issues/44721#issuecomment-330945507&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="org8c880cb"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Where's the type theory?
&lt;/h2&gt;

&lt;p&gt;People have tried to explain it this way (existential? universal?), but take a step back to look at rust. &lt;code&gt;impl Trait&lt;/code&gt; is a single step forward in type inference.&lt;/p&gt;

&lt;p&gt;Recognize that I'm not using a powerful set of tools for understanding type systems that does exist. Give this article a read:&lt;br&gt;
&lt;a href="https://varkor.github.io/blog/2018/07/03/existential-types-in-rust.html" rel="noopener noreferrer"&gt;https://varkor.github.io/blog/2018/07/03/existential-types-in-rust.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a id="org642d739"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Applied example (return position)
&lt;/h2&gt;

&lt;p&gt;Naive attempt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;make_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;make_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;make_value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Display&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Hello, World"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Hello, World (1)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;panic!&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fails because…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    error[E0282]: type annotations needed
     --&amp;gt; /home/sam/code/trash/impl-return.rs:4:20
      |
    4 |     println!("{}", make_value(0));
      |                    ^^^^^^^^^^ cannot infer type for `T`

    error[E0308]: match arms have incompatible types
      --&amp;gt; /home/sam/code/trash/impl-return.rs:9:5
       |
    9  | /     match index {
    10 | |         0 =&amp;gt; "Hello, World",
       | |              -------------- match arm with an incompatible type
    11 | |         1 =&amp;gt; "Hello, World (1)",
    12 | |         _ =&amp;gt; panic!(),
    13 | |     }
       | |_____^ expected type parameter, found reference
       |
       = note: expected type `T`
                  found type `&amp;amp;'static str`

    error: aborting due to 2 previous errors

    Some errors occurred: E0282, E0308.
    For more information about an error, try `rustc --explain E0282`.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, so I do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;make_value&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;make_value&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;make_value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Display&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Hello, World"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"okay"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;panic!&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another dead end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    error[E0308]: match arms have incompatible types
      --&amp;gt; /home/sam/code/trash/impl-return.rs:9:5
       |
    9  | /     match index {
    10 | |         0 =&amp;gt; "Hello, World",
       | |              -------------- match arm with an incompatible type
    11 | |         1 =&amp;gt; "okay",
    12 | |         _ =&amp;gt; panic!(),
    13 | |     }
       | |_____^ expected type parameter, found reference
       |
       = note: expected type `T`
                  found type `&amp;amp;'static str`

    error: aborting due to previous error

    For more information about this error, try `rustc --explain E0308`.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yet this succeeds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;make_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Display&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Hello, World"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Hello, World (1)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;panic!&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Generated using org-mode)&lt;/p&gt;

</description>
      <category>rust</category>
      <category>learning</category>
      <category>examples</category>
    </item>
    <item>
      <title>Rust Module Essentials</title>
      <dc:creator>Sam Pagenkopf</dc:creator>
      <pubDate>Tue, 19 Dec 2017 04:35:11 +0000</pubDate>
      <link>https://forem.com/hertz4/rust-module-essentials-12oi</link>
      <guid>https://forem.com/hertz4/rust-module-essentials-12oi</guid>
      <description>&lt;p&gt;Do you remember the first time you made a project with more than one source file? Was it with headers, with classes...or, with modules?&lt;/p&gt;

&lt;p&gt;Modules have certain traits. They allow various items to coexist in a unit, without being bound to a class. A module file generally has its interface (outward-facing public parts) on top, and its implementation (dirty inner details) on the bottom.&lt;/p&gt;

&lt;p&gt;Understanding Rust's modules comes from understanding the keywords &lt;code&gt;mod&lt;/code&gt;, &lt;code&gt;pub&lt;/code&gt;, and &lt;code&gt;use&lt;/code&gt;. To state them simply:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mod&lt;/code&gt; declares a module.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pub&lt;/code&gt; exposes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;use&lt;/code&gt; pulls things in.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's begin with an example. Modules can be defined two ways. First in place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.rs&lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Good."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;food&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, as a separate file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.rs&lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;food&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// food.rs or food/mod.rs&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Yum."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So more specifically:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;mod X&lt;/code&gt; means: let there be a module X, defined either here, in braces, or in a separate file named &lt;code&gt;X.rs&lt;/code&gt; or &lt;code&gt;X/mod.rs&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;pub fn eat&lt;/code&gt; makes &lt;code&gt;eat&lt;/code&gt; visible to main. Without &lt;code&gt;pub&lt;/code&gt;, main would not be able to call &lt;code&gt;eat&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note that without &lt;code&gt;mod food&lt;/code&gt; in main, Rust would ignore &lt;code&gt;food.rs&lt;/code&gt; entirely.&lt;/p&gt;

&lt;p&gt;Next, let's expand the above to something more deeply nested:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.rs&lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;lunch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Take a break."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;food&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;lunch&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, as multiple files. Note the use of &lt;code&gt;mod.rs&lt;/code&gt;, a special filename:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.rs&lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;food&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;lunch&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// food/mod.rs&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;lunch&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// food/lunch.rs&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hamburger."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Does this look like you expected? Notice how both &lt;code&gt;fn eat&lt;/code&gt; and &lt;code&gt;mod lunch&lt;/code&gt; are made &lt;code&gt;pub&lt;/code&gt;. They must be carried up this way, one level at a time.&lt;/p&gt;

&lt;p&gt;And of course, anything without &lt;code&gt;pub&lt;/code&gt; is invisible outside its module.&lt;/p&gt;

&lt;p&gt;Perhaps surprisingly, &lt;code&gt;pub&lt;/code&gt; treats struct members and methods on an individual basis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// food.rs&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Meal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;taste&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// cannot see price&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Meal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I taste {}."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.taste&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;purchase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// cannot call purchase directly&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Spent ${}. Ouch."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.price&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Having a private variable &lt;code&gt;price&lt;/code&gt; means that you cannot make a &lt;code&gt;Meal&lt;/code&gt; outside of &lt;code&gt;food&lt;/code&gt;. A constructor method can help with that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// food.rs&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Meal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;fancy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;Self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Meal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;taste&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"miracles on plate"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;44100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// main.rs&lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;lunch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;food&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fancy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;lunch&lt;/span&gt;&lt;span class="nf"&gt;.eat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, to recap:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;pub&lt;/code&gt; exposes an item, either on module or struct level, to its surrounding level.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Do note that &lt;code&gt;Meal::fancy&lt;/code&gt; could also be named &lt;code&gt;Meal::new&lt;/code&gt;, or even other things.&lt;/p&gt;

&lt;p&gt;Next, let's examine &lt;code&gt;use&lt;/code&gt; to complete our set of three. &lt;code&gt;use&lt;/code&gt; is a tool that pulls in words to reduce visual clutter and finger work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;food&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;lunch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fancy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;lunch&lt;/span&gt;&lt;span class="nf"&gt;.eat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or even:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;food&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fancy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;lunch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fancy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;lunch&lt;/span&gt;&lt;span class="nf"&gt;.eat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pulling in &lt;code&gt;fancy&lt;/code&gt; directly is an extreme example, but it does work. In the same way that &lt;code&gt;pub&lt;/code&gt; can apply to structs, so can &lt;code&gt;use&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A wrinkle with &lt;code&gt;use&lt;/code&gt; is that it has absolute paths. To understand paths, consider this non-working example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;cheaper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;cmp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="py"&gt;.price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="py"&gt;.price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This fails because &lt;code&gt;std::cmp&lt;/code&gt; is the same as writing &lt;code&gt;self::std::cmp&lt;/code&gt;, &lt;code&gt;self&lt;/code&gt; here being &lt;code&gt;food&lt;/code&gt;. It's a relative path. So, let's use an absolute path by adding &lt;code&gt;::&lt;/code&gt; to the start.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;cheaper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;cmp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="py"&gt;.price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="py"&gt;.price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works, but the &lt;code&gt;use&lt;/code&gt; keyword is also an absolute path, and can reduce typing in the long run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;cheaper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;cmp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="py"&gt;.cost&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="py"&gt;.cost&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's another way of doing it. Yes, &lt;code&gt;use&lt;/code&gt; does respect its scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;cheaper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;cmp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="py"&gt;.cost&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="py"&gt;.cost&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is some light alternate syntax for &lt;code&gt;use&lt;/code&gt; as well, in order to pull in multiple things.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;bread&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Slice&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
       &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Loaf&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Plate&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
   &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Napkin&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;food&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;bread&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// pulls in Slice and Loaf&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;food&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;Plate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Napkin&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, to be specific:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;use&lt;/code&gt; brings module or struct items into the current scope from an absolute path.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also note that &lt;code&gt;pub use&lt;/code&gt; can be used as a flattening method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;breakfast&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;cereal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Snip, crankle, porp."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;breakfast&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cereal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;food&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;cereal&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This enables you to divide into further modules for organization, without adding a burden to the outside world.&lt;/p&gt;

&lt;p&gt;Overall, it is better to be more explicit (avoid too much &lt;code&gt;*&lt;/code&gt;) and &lt;code&gt;use&lt;/code&gt; judiciously. It's better to know where things are coming from when you see them later, but added visual clutter and typing are no fun.&lt;/p&gt;

&lt;p&gt;To recap:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mod&lt;/code&gt; declares a module.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pub&lt;/code&gt; exposes an item by a single level.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;use&lt;/code&gt; brings things from an absolute path to the current scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is more, but I hope I have helped get things started. Let me know if you're interested in a second part.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>module</category>
      <category>examples</category>
      <category>howto</category>
    </item>
  </channel>
</rss>
