<?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>DEV Community: Jorge Luis Perez</title>
    <description>The latest articles on DEV Community by Jorge Luis Perez (@jolisper).</description>
    <link>https://dev.to/jolisper</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%2F2544091%2Fa42f73f2-d12a-4ef6-b288-48f498f863d4.jpeg</url>
      <title>DEV Community: Jorge Luis Perez</title>
      <link>https://dev.to/jolisper</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jolisper"/>
    <language>en</language>
    <item>
      <title>Modular Java Builds with Gradle: From Setup to Strategy</title>
      <dc:creator>Jorge Luis Perez</dc:creator>
      <pubDate>Sun, 06 Apr 2025 16:24:13 +0000</pubDate>
      <link>https://dev.to/jolisper/modular-java-builds-with-gradle-from-setup-to-strategy-17nh</link>
      <guid>https://dev.to/jolisper/modular-java-builds-with-gradle-from-setup-to-strategy-17nh</guid>
      <description>&lt;p&gt;In this hands-on guide we'll see how we can configure composite builds in a simple way using the Gradle command line utils as much as possible — plus a bit of how I used them to break up a monolith on the way to microservices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Composite Builds?
&lt;/h2&gt;

&lt;p&gt;Composite builds are a powerful Gradle feature that address the challenges of modular development, particularly in projects that need to evolve independently but still integrate reliably. They offer several practical advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Live dependency substitution&lt;/strong&gt;: You can replace the typical change–publish–install workflow with a direct project dependency. This means that any changes you make to a library are immediately visible to the consuming application—no need to publish intermediate versions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modular development within monorepos&lt;/strong&gt;: Composite builds let you split a monorepo into multiple independent builds. You can work with each module in isolation for faster feedback, and use the full composite to verify everything still works together.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Temporary or ad hoc integration&lt;/strong&gt;: You can integrate separately developed projects without merging their structures. This is especially useful for testing changes across repositories or coordinating temporary collaborations before publishing a new version.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Scaffolding the Projects
&lt;/h2&gt;

&lt;p&gt;Let's create the root directory that will contain the projects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;composite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we'll create and application and a library projects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;composite
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;application
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;library
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;tree&lt;/code&gt; command run should return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── application
└── library

2 directories, 0 files
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the &lt;code&gt;gradle init&lt;/code&gt; command to create the structure for the projects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;application
&lt;span class="nv"&gt;$ &lt;/span&gt;gradle init &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--type&lt;/span&gt; java-application &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--dsl&lt;/span&gt; kotlin &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--test-framework&lt;/span&gt; junit-jupiter &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--package&lt;/span&gt; com.example.app &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--project-name&lt;/span&gt; application &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--no-split-project&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--java-version&lt;/span&gt; 21 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--incubating&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next the library project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ../library
&lt;span class="nv"&gt;$ &lt;/span&gt;gradle init &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--type&lt;/span&gt; java-library &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--dsl&lt;/span&gt; kotlin &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--test-framework&lt;/span&gt; junit-jupiter &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--package&lt;/span&gt; com.example.lib &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--project-name&lt;/span&gt; library &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--no-split-project&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--java-version&lt;/span&gt; 21 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--incubating&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, a &lt;code&gt;tree -d -L 4&lt;/code&gt; command run from the root directory should return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── application
│   ├── app
│   │   └── src
│   │       ├── main
│   │       └── test
│   └── gradle
│       └── wrapper
└── library
    ├── gradle
    │   └── wrapper
    └── lib
        └── src
            ├── main
            └── test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we've the basic structure of two unconnected projects. Let's make a composite build with them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Composite Build Configuration
&lt;/h2&gt;

&lt;p&gt;From the root directory, we'll create an empty Gradle settings file so we can generate the Gradle wrapper.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;settings.gradle.kts
&lt;span class="nv"&gt;$ &lt;/span&gt;gradle wrapper
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, in &lt;code&gt;settings.gradle.kts&lt;/code&gt; at the root directory, we must inform to Gradle which builds are included in the composite:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ../composite/settings.gradle.kts&lt;/span&gt;
&lt;span class="n"&gt;rootProject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"composite"&lt;/span&gt;

&lt;span class="nf"&gt;includeBuild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"application"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;includeBuild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"library"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test our configuration, run the following from the root project directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./gradlew projects
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That should return the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Task :projects

Projects:

------------------------------------------------------------
Root project 'composite'
------------------------------------------------------------

Root project 'composite'
No sub-projects

Included builds:

+--- Included build ':application'
\--- Included build ':library'

To see a list of the tasks of a project, run gradlew &amp;lt;project-path&amp;gt;:tasks
For example, try running gradlew :tasks

BUILD SUCCESSFUL in 789ms
1 actionable task: 1 executed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the projects are included in the same composite, but still unrelated without any real dependency between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting the Builds
&lt;/h2&gt;

&lt;p&gt;Let's use the library in the application project. In order to do that we need to edit the &lt;code&gt;build.gradle.kts&lt;/code&gt; files in the &lt;code&gt;lib&lt;/code&gt; and in the &lt;code&gt;app&lt;/code&gt; modules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ../composite/library/lib/build.gradle.kts&lt;/span&gt;
&lt;span class="n"&gt;group&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"com.example"&lt;/span&gt;
&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.0.1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, in the application module, we can add the library as a dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ../composite/application/app/build.gradle.kts&lt;/span&gt;

&lt;span class="nf"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.example:lib:0.0.1"&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;One interesting thing to notice here is that we declared the library as if it were a binary dependency, using its group, name, and version. From the application’s perspective, it looks just like any third-party dependency — but with one huge advantage: thanks to composite builds, Gradle automatically substitutes it with the local project source. Any change made in the library is immediately seen by the application, without needing to publish or install anything. That’s the magic of composite builds.&lt;/p&gt;

&lt;p&gt;Let's make some use of the library from the app code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.example.app&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.example.lib.Library&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Import the library&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getGreeting&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Library&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getGreeting&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Get the greeating from library&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getGreeting&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;Library&lt;/code&gt;, just add the necessary method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.example.lib&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Library&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getGreeting&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello from Libray!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's run &lt;code&gt;app&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./gradlew :application:app:run                                                                                            
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Task :application:app:run
Hello from Libray!

BUILD SUCCESSFUL in 1s
4 actionable tasks: 1 executed, 3 up-to-date
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we then change the library greeting message and run the app again, the app will notice the change immediately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Library&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getGreeting&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello there!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voila!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Task :application:app:run
Hello there!

BUILD SUCCESSFUL in 10s
4 actionable tasks: 3 executed, 1 up-to-date
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Other Considerations
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Internal Wrappers
&lt;/h4&gt;

&lt;p&gt;We used the &lt;code&gt;gradle init&lt;/code&gt; command to generate the project structure, which also created a Gradle wrapper (&lt;code&gt;./gradlew&lt;/code&gt;). You should keep an internal Gradle wrapper in a project &lt;strong&gt;only if that project is meant to be developed or used independently&lt;/strong&gt; — for example, if it’s published separately, run in its own CI pipeline, or needs to pin a different Gradle version. In typical composite builds or multi-project setups where everything is built together from the root, you don’t need internal wrappers — they just add clutter. Keep a single wrapper at the root unless there's a strong reason not to.&lt;/p&gt;

&lt;h4&gt;
  
  
  Internal Versions Catalogs
&lt;/h4&gt;

&lt;p&gt;The same consideration applies to version catalogs. You should define a &lt;code&gt;libs.versions.toml&lt;/code&gt; file in an included build or individual project &lt;strong&gt;only if that project is intended to be consumed in isolation&lt;/strong&gt;. If you're working in a composite build where modules are developed together, it's best to define a single shared catalog in the root build. This avoids duplication, reduces maintenance overhead, and keeps dependency versions consistent across builds. As with wrappers, use separate version catalogs only when you genuinely need independence.&lt;/p&gt;

&lt;p&gt;By default, the &lt;code&gt;libs.versions.toml&lt;/code&gt; file is placed in the &lt;code&gt;gradle/&lt;/code&gt; directory alongside the wrapper JAR in every generated module. If you want to use a single shared version catalog, you'll need to add an extra piece of configuration to the &lt;code&gt;settings.gradle.kts&lt;/code&gt; file of each included build: the &lt;code&gt;dependencyResolutionManagement&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;rootProject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"library"&lt;/span&gt;

&lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"lib"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;dependencyResolutionManagement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;versionCatalogs&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"libs"&lt;/span&gt;&lt;span class="p"&gt;)&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="nf"&gt;files&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"../gradle/libs.versions.toml"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the &lt;code&gt;libs&lt;/code&gt; version catalog refers to the &lt;code&gt;libs.vesions.toml&lt;/code&gt; file in the root directory and you can safety remove the ones inside the modules. &lt;/p&gt;

&lt;h4&gt;
  
  
  Root Build Script
&lt;/h4&gt;

&lt;p&gt;One last thing you should consider is adding a build script in the composite's root directory to define some global tasks. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;defaultTasks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"run"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"run"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;dependsOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gradle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includedBuild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"application"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":app:run"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testAll"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;dependsOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gradle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includedBuild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"application"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":app:test"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nf"&gt;dependsOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gradle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includedBuild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"library"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":lib:test"&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 setup allows you to run &lt;code&gt;./gradlew testAll&lt;/code&gt; from the root directory to execute all tests across the composite build.&lt;/p&gt;

&lt;p&gt;If you decide to use a single wrapper, a single version catalog and a root build script you'll end up with the following directory structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── application
│   ├── app
│   │   ├── build.gradle.kts
│   │   └── src
│   │       ├── main
│   │       │   ├── java
│   │       │   │   └── org
│   │       │   │       └── example
│   │       │   │           └── App.java
│   │       │   └── resources
│   │       └── test
│   │           ├── java
│   │           │   └── org
│   │           │       └── example
│   │           │           └── AppTest.java
│   │           └── resources
│   └── settings.gradle.kts
├── build.gradle.kts
├── gradle
│   ├── libs.versions.toml
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
│   ├── lib
│   │   ├── build.gradle.kts
│   │   └── src
│   │       ├── main
│   │       │   ├── java
│   │       │   │   └── org
│   │       │   │       └── example
│   │       │   │           └── Library.java
│   │       │   └── resources
│   │       └── test
│   │           ├── java
│   │           │   └── org
│   │           │       └── example
│   │           │           └── LibraryTest.java
│   │           └── resources
│   └── settings.gradle.kts
└── settings.gradle.kts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Composites in a Monolith-to-Microservices Scenario
&lt;/h2&gt;

&lt;p&gt;A few years ago, I was working on a system that had started to show serious scalability issues — both in terms of performance and design flexibility. It followed a classic three-layer architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A REST API layer that handled requests from the UI and external systems&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A middle layer containing the business logic&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An integration layer responsible for communicating with various third-party providers&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The integration layer, in particular, had become a bottleneck. It was monolithic in structure and hard to evolve. Performance was another challenge — each provider had very different response characteristics, which made it difficult to scale or even size that layer appropriately.&lt;/p&gt;

&lt;p&gt;We knew we needed to evolve toward a more modular architecture, but a full rewrite wasn’t realistic. Instead, we chose a path of &lt;strong&gt;incremental decomposition&lt;/strong&gt;. The goal was to isolate provider-specific logic that could be developed and tested independently, without breaking the existing deployment model.&lt;/p&gt;

&lt;p&gt;To enable this, we adopted Gradle composite builds. Rather than splitting the integration layer into separate microservices immediately — which would have required significant coordination, deployment, and CI complexity — we began extracting provider connectors into standalone Gradle builds. These builds could be version-controlled separately, tested in isolation, and evolved at their own pace.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;includeBuild(...)&lt;/code&gt;, we wired them back into the main system as source dependencies. This gave us the benefits of modularity without forcing early service boundaries or dependency publication. Because Gradle treated them as if they were published artifacts, nothing had to change in the rest of the system.&lt;/p&gt;

&lt;p&gt;To keep the business logic layer insulated from these changes, we introduced a small internal SDK — a module that exposed a stable interface to the provider connectors. It encapsulated provider selection logic and served as a boundary between the modularized integration code and the core application. In some cases, the SDK imported the connector directly using &lt;code&gt;includeBuild(...)&lt;/code&gt;; in others, it routed requests over HTTP to connectors that had been promoted to microservices. This dual-mode setup allowed us to evolve the architecture incrementally behind a unified interface, without impacting the rest of the system. It also helped maintain a consistent developer experience throughout the transition.&lt;/p&gt;

&lt;p&gt;This approach gave us several advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We could work on or refactor connectors without having to touch unrelated parts of the system, because each connector lived in its own isolated build and interacted with the rest of the application only through a stable SDK interface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Teams could build and test connectors independently, which sped up local development and reduced unnecessary work in CI, because the connectors were split into separate builds with their own lifecycle and caching behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Over time, we could graduate some of these connectors into fully independent microservices — often without changing their internal APIs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This setup wasn’t without tradeoffs. It introduced more build logic and required engineers to become comfortable working across multiple Gradle builds. But the gains in modularity, velocity, and long-term flexibility far outweighed the initial complexity.&lt;/p&gt;

&lt;p&gt;In practice, composite builds gave us an architectural bridge between monolith and microservices. They enabled controlled, observable evolution — allowing us to restructure the system without stopping the world.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Gradle composite builds offer a clean and flexible way to work with multiple independent projects as if they were part of a single unified system. In this tutorial, we used &lt;code&gt;gradle init&lt;/code&gt; to scaffold the project structure, configured the composite with &lt;code&gt;includeBuild&lt;/code&gt;, and wired everything together using a shared wrapper, version catalog, and root build script. This setup allows changes in one project to be immediately reflected in others, without the need to publish or install artifacts — enabling faster feedback loops and smoother cross-project collaboration.&lt;/p&gt;

&lt;p&gt;Beyond simple modularization, composite builds can also serve as a practical bridge during architectural transitions. As shown in the monolith-to-microservices example, they make it possible to incrementally extract parts of a system and defer deployment decisions without sacrificing testability or development speed. Whether you're modernizing a legacy system or building modular applications from scratch, composite builds give you the structure to scale with confidence — both technically and organizationally.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Licensed under &lt;a href="https://creativecommons.org/licenses/by-sa/4.0/" rel="noopener noreferrer"&gt;CC BY-SA 4.0&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;References&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.gradle.org/introducing-composite-builds?utm_source=chatgpt.com" rel="noopener noreferrer"&gt;Introducing Composite Builds&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.gradle.org/current/userguide/composite_builds.html" rel="noopener noreferrer"&gt;Composite Builds&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>gradle</category>
      <category>microservices</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Rust Key Concepts (I): Ownership System</title>
      <dc:creator>Jorge Luis Perez</dc:creator>
      <pubDate>Fri, 07 Feb 2025 20:57:36 +0000</pubDate>
      <link>https://dev.to/jolisper/rust-key-concepts-i-ownership-system-1mnm</link>
      <guid>https://dev.to/jolisper/rust-key-concepts-i-ownership-system-1mnm</guid>
      <description>&lt;p&gt;Rust has specific design choices and feature selections that make the language unique. Let’s dive into the key concepts that lead us to a deeper understanding of the language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ownership System
&lt;/h2&gt;

&lt;p&gt;Ownership is hands down one of the first big ideas a newcomer to Rust has to wrap their head around. To really grasp Rust's ownership system, some background knowledge about variables, memory regions, and memory management is essential. This foundation will help you develop a solid mental model of what's going on. However, instead of diving into all that theory right away, let's start by building a practical sense of how the ownership system works. Let's set up a problem scenario and see how we can tackle it.&lt;/p&gt;

&lt;p&gt;In Rust, when we write something 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;let&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are doing more than simply assigning the “Hello Rust” string to the variable &lt;em&gt;message&lt;/em&gt;. We’re binding the string — the value — to a scope.&lt;/p&gt;

&lt;p&gt;Which scope? The one where the variable is defined.&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="p"&gt;{&lt;/span&gt;  
   &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&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;Statically, the syntax defines the scope as the block enclosed by the opening and closing braces.&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="n"&gt;t0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
&lt;span class="n"&gt;t1&lt;/span&gt;   &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
&lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="n"&gt;t3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dynamically speaking, the execution flow proceeds top-down, from the opening brace to the closing brace, spanning from &lt;em&gt;t0&lt;/em&gt; to &lt;em&gt;t3&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;At &lt;em&gt;t0,&lt;/em&gt; a new scope is defined.&lt;/p&gt;

&lt;p&gt;At &lt;em&gt;t1,&lt;/em&gt; a variable message is declared and initialized, allocating memory for the string. The lifetime of the string value is also bound to the scope of the variable message.&lt;/p&gt;

&lt;p&gt;When execution reaches &lt;em&gt;t2,&lt;/em&gt; the variable &lt;em&gt;message&lt;/em&gt; goes out of scope, and the value associated with the variable is dropped from memory, deallocating the memory reserved at &lt;em&gt;t1&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;At &lt;em&gt;t3,&lt;/em&gt; the program flow continues after the block, but the &lt;em&gt;message&lt;/em&gt; variable is no longer accessible because it was dropped when the block exited.&lt;/p&gt;

&lt;p&gt;In Rust parlance, we would say that &lt;em&gt;The message variable is the owner of the string ‘Hello Rust!’.&lt;/em&gt; Technically, Rust couples the lifetime of a value in memory with the scope of the variable associated with that value.&lt;/p&gt;

&lt;p&gt;If you come from a memory-managed language, this behavior might not impress you because it’s exactly what you expect — you don’t even need to think about it. However, the key difference is that this mechanism is determined at compile time. Rust shifts the memory management problem from run time to compile time, so you don’t incur extra run time overhead for automatic memory management.&lt;/p&gt;

&lt;p&gt;Another important distinction compared to a typical garbage collection (GC) algorithm is that Rust’s memory management is deterministic — you know exactly when a value will be dropped. In contrast, with a GC, you can’t predict precisely when it will pause your application thread to perform garbage collection.&lt;/p&gt;

&lt;p&gt;From the perspective of a C programmer, the benefits are more tangible because, in C, memory management is manual. With Rust, the compiler prevents many common problems associated with manual memory management, such as use-after-free errors, double-free errors, some memory leaks, dangling pointers, and out-of-bounds indexes.&lt;/p&gt;

&lt;p&gt;As we can see, the semantics of assigning a value to a variable in Rust are denser than in other programming languages because they implicitly include an ownership declaration.&lt;/p&gt;

&lt;p&gt;Rust has clear rules about ownership:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each value in Rust has an &lt;em&gt;owner&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;There can only be one owner at a time.&lt;/li&gt;
&lt;li&gt;When the owner goes out of scope, the value will be dropped.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These rules are checked at compile time, which means that if any of them are violated, we’ll get a compilation error, preventing us from generating an executable with memory management deficiencies.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rust shifts the memory management problem from run time to compile time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s keep that in mind and explore how the ownership system affects Rust’s programming.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ownership Moving
&lt;/h3&gt;

&lt;p&gt;Here is a slightly more complex code:&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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="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;"{message}"&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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;The &lt;em&gt;message&lt;/em&gt; variable is assigned in the main function's scope and passed as an argument to another function that prints the message. This code compiles successfully, meaning all ownership rules are upheld.&lt;/p&gt;

&lt;p&gt;The program not only compiles but also runs without any issues.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo run  
   Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s  
    Running `target/debug/rust-kc-p1`  
Hello Rust!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we want to print not only the message but also its size after printing the message itself.&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printable_message&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="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;"{message}"&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;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sized_message&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="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="n"&gt;message&lt;/span&gt;&lt;span class="nf"&gt;.len&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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;To achieve this, we added another function to calculate and print the size, calling it after the first message was printed. However, there’s a problem: the compiler complains about our latest addition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo run  
  Compiling rust-kc-p1 v0.1.0 (/home/jolisper/src/rust-key-concepts/rust-kc-p1)  
error[E0382]: use of moved value: `message`  
 --&amp;gt; src/main.rs:16:24  
   |  
12 |     let message = String::from("Hello Rust!");  
   |         ------- move occurs because `message` has type `String`, which does not implement the `Copy` trait  
13 |  
14 |     print_message(message);  
   |                   ------- value moved here  
15 |  
16 |     print_message_size(message);  
   |                        ^^^^^^^ value used here after move  
   |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rust’s compiler error messages are usually helpful and often suggest ways to fix the problem. But to get the most out of them, you need a good grasp of how the ownership system works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Every time the Rust compiler stops you, it’s because a design decision must be taken.&lt;/strong&gt; Any design decision comes with its trade-offs. My advice here is not to rush and to solve the problem quickly or blindly follow the suggestions. Instead, invest the time to really understand why the error occurs; it is the best option in the long run.&lt;/p&gt;

&lt;p&gt;I will say it again.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rust shifts the memory management problem from run time to compile time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That means we have to solve more problems before our program can run. Spending more time on this part of the process is completely natural. Keep that in mind the next time the anxiety of getting your program running starts creeping up your fingertips.&lt;/p&gt;

&lt;p&gt;Coming back to the code, the key thing here is that the compiler tells us we’re trying to use a &lt;em&gt;moved value&lt;/em&gt;. What does this mean? Let’s revisit Rust’s ownership rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each value in Rust has an &lt;em&gt;owner&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;There can only be one owner at a time.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;When the owner goes out of scope, the value will be dropped.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s focus on the second rule. This clearly states that there can only be one owner for each value, but this doesn’t mean the owner must remain the same throughout the entire program’s execution. In fact, ownership can change hands during the program’s execution. For instance, when a value is assigned to another variable, added to a vector, or stored on the heap, ownership of the value moves from its original location to the new one. And that’s exactly what happens in this case.&lt;/p&gt;

&lt;p&gt;When we first bind the &lt;em&gt;“Hello Rust!”&lt;/em&gt; string to the variable &lt;em&gt;message&lt;/em&gt;, that variable becomes its owner. However, when we pass the &lt;em&gt;message&lt;/em&gt; variable as an argument for the &lt;em&gt;print_message()&lt;/em&gt; function, the ownership of the value is transferred to the function’s parameter variable, &lt;em&gt;printable_message&lt;/em&gt;. As a result, the original variable, &lt;em&gt;message&lt;/em&gt;, loses its ownership of the value because it was moved. After this, the &lt;em&gt;message&lt;/em&gt; variable in the main function is no longer valid, and attempting to use it again will result in a compilation error.&lt;/p&gt;

&lt;p&gt;It's important to note that the scope of the string &lt;em&gt;“Hello Rust!”&lt;/em&gt; changes when ownership is transferred. The new scope is defined by the &lt;em&gt;print_message&lt;/em&gt; function. This means that when the &lt;em&gt;print_message&lt;/em&gt; function returns, the &lt;em&gt;printable_message&lt;/em&gt; parameter goes out of scope, resulting in the string being dropped. In cases like this, it is common to say that a function &lt;em&gt;consumes&lt;/em&gt; the value.&lt;/p&gt;

&lt;p&gt;The process described in the previous paragraph also applies to the first version of the code, where the &lt;em&gt;print_message()&lt;/em&gt; function is the only additional function defined. However, since there is no attempt to reuse the &lt;em&gt;message&lt;/em&gt; variable, no ownership rules are violated, and the code compiles successfully.&lt;/p&gt;

&lt;p&gt;From here, things get interesting because Rust begins to reveal its full arsenal. There are several ways to resolve the issue, each with its trade-offs. It’s up to us, the programmers, to decide which path is best for our problem.&lt;/p&gt;

&lt;p&gt;The ownership system is so prevalent in Rust that talking about it implies touching several other language features and concepts. To keep ownership at the center, in the rest of the article, we'll touch on them briefly, without deep dives, just to explain different aspects of the ownership system. You might feel tempted to take a bigger detour into those topics when that happens, but my humble advice is to take a deep breath, relax, and keep reading to build a practical sense of how the ownership system works before jumping to them. Consider these as glimpses into future articles.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use of Moved Value
&lt;/h3&gt;

&lt;p&gt;Now that we've set up the problem, we’re ready to dive into the options for solving it. We’re not aiming to cover every possible solution here. Instead, we’ll explore a handful of approaches that let us dig into the nuances of the ownership system and put the basics we covered earlier to the test.&lt;/p&gt;

&lt;h4&gt;
  
  
  Return Ownership Back
&lt;/h4&gt;

&lt;p&gt;One way to overcome the use of a moved value error is to make sure that the function returns the string passed as an argument and use that return value to call the other function.&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printable_message&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="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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{printable_message}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
  &lt;span class="n"&gt;printable_message&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;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sized_message&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="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="n"&gt;sized_message&lt;/span&gt;&lt;span class="nf"&gt;.len&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="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;Also, since the value is returned, we can avoid the intermediate assign of the message variable and chain the functions calls:&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;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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;While this might not be the first option in practice, it demonstrates how a function can take and return ownership of a value. It’s important to note that this process does not require any additional allocation.&lt;/p&gt;

&lt;p&gt;In terms of ownership, we are adding an extra moving step to solve the issue: the returned ownership in the &lt;em&gt;print_message&lt;/em&gt; function. Interestingly, the value itself isn’t what matters here. For example, we can return a completely different &lt;em&gt;String&lt;/em&gt; value from the function, and the code still compiles:&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printable_message&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="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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{printable_message}"&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;"Another string value"&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;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sized_message&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="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="n"&gt;sized_message&lt;/span&gt;&lt;span class="nf"&gt;.len&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="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;Of course, this breaks the functionality, as we are now printing the size of a new &lt;em&gt;String&lt;/em&gt; rather than the original one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Rust!  
20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This reveals an important point: under Rust’s ownership rules, the specific values assigned to variables are irrelevant. What truly matters is the &lt;em&gt;ownership flow&lt;/em&gt; through the variables and function calls.&lt;/p&gt;

&lt;h5&gt;
  
  
  Ownership and Mutability
&lt;/h5&gt;

&lt;p&gt;Rust does not provide immutable values out of the box but provides immutable variables by default. This variable mutability condition is inherited by the value that the variable refers to, which means that a value behind an immutable variable is immutable, too&lt;sup id="fnref1"&gt;1&lt;/sup&gt;. If we want to declare a variable as mutable, we must do it explicitly using the &lt;code&gt;mut&lt;/code&gt; keyword in the variable declaration. This principle that transmit the mutability condition of a variable to the value behind it is called: &lt;em&gt;inherited mutability&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In the example that serves as a problem scenario, we are using an immutable variable that refers to a String value holding the message to print out. But observe the following subtle change.&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;printable_message&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printable_message&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" From a function"&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;"{printable_message}"&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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;When ownership is moved, the function that takes ownership can decide to change the mutability condition of that value. In this case, although the variable passed as argument is immutable, the function decided to change that condition and made the value mutable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Rust! From a function.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is another version using shadowing.&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printable_message&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="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;"{printable_message}"&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;scrolling&lt;/span&gt; &lt;span class="o"&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;"A long time ago "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// scrolling.push_str("far far away"); // error: cannot mutate immutable variable value&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;scrolling&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scrolling&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;scrolling&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"in a galaxy far, far away..."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scrolling&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 shows us an important relation between mutability and ownership: when they are moved, owned values can be switched from immutable to mutable.&lt;/p&gt;

&lt;h4&gt;
  
  
  Cloning
&lt;/h4&gt;

&lt;p&gt;Another way to overcome the issue is to simply avoid moving the value by passing a clone of the original instead.&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printable_message&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="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;"{printable_message}"&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;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sized_message&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="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="n"&gt;sized_message&lt;/span&gt;&lt;span class="nf"&gt;.len&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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

   &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;In this version, we create a copy of the original string and pass that copy as an argument to the &lt;em&gt;print_message&lt;/em&gt; function. No ownership transfer happens in this case, but we are expending the double memory until the end of &lt;em&gt;print_message()&lt;/em&gt; because we are allocating a copy of the string.&lt;/p&gt;

&lt;p&gt;The need to explicitly call the &lt;em&gt;clone()&lt;/em&gt; function for cloning a value is considered an advantage because it’s made explicit that the cloning occurs. To make this call, the type must implement the &lt;em&gt;Clone&lt;/em&gt; trait. If we define our custom type, such as a struct, and all its fields implement &lt;em&gt;Clone&lt;/em&gt;, the implementation is derivable.&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="nd"&gt;#[derive(Clone)]&lt;/span&gt;  
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
   &lt;span class="n"&gt;name&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;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u8&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this type of solution, evaluating the cost of the cloning is important. If the value is small, like in this example, the performance cost could be irrelevant, but in other cases where the value is big or if the cloning is inside a loop creating a lot of copies, the cost may represent too much.&lt;/p&gt;

&lt;p&gt;Cloning everywhere is considered an anti-pattern in Rust because calling the &lt;em&gt;clone()&lt;/em&gt; function introduces performance overhead, as every call creates a new memory allocation. Let’s explore alternative solutions to the problem scenario and return to this later.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reference Counting
&lt;/h4&gt;

&lt;p&gt;Let’s revisit the second rule of the ownership system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There can only be one owner at a time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This rule is concise and clear, but, put differently, what’s truly restricted is shared ownership of a value. At times, this rule might feel overly restrictive and could hinder valid patterns. For example, if we need to share an immutable value, what’s the issue with sharing its ownership?&lt;/p&gt;

&lt;p&gt;In Rust, the reference counting smart pointer, &lt;em&gt;Rc&amp;lt;T&amp;gt;&lt;/em&gt;, provides shared ownership of type &lt;em&gt;T&lt;/em&gt;. Reference counting is a memory management technique where an allocated value keeps a count of how many references (or owners) point to it.&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;rc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Rc&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printable_message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;"{printable_message}"&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;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sized_message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="n"&gt;sized_message&lt;/span&gt;&lt;span class="nf"&gt;.len&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Rc&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="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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

   &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

   &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="nf"&gt;.clone&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;When you clone an &lt;em&gt;Rc&amp;lt;T&amp;gt;&lt;/em&gt;, another pointer to the same allocation is created, and the reference count is incremented&lt;sup id="fnref2"&gt;2&lt;/sup&gt;. When an &lt;em&gt;Rc&amp;lt;T&amp;gt;&lt;/em&gt; goes out of scope, its drop logic decrements the reference count. If the count reaches zero, the value is deallocated.&lt;/p&gt;

&lt;p&gt;This means that &lt;em&gt;Rc&lt;/em&gt; does not create a copy of the value &lt;em&gt;T&lt;/em&gt;; it merely increments or decrements the reference count. As a result, this approach introduces some overhead because &lt;em&gt;Rc&lt;/em&gt; needs to keep track of how many owners the value &lt;em&gt;T&lt;/em&gt; has. In situations where we want or need to share the same instance of an object across different parts of a program, the &lt;em&gt;Rc&lt;/em&gt; smart pointer comes in handy.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using References
&lt;/h4&gt;

&lt;p&gt;In the previous solutions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We return ownership back, using a function that takes and gives ownership back to the caller.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid ownership move by passing a deep copy of the value to the function with &lt;code&gt;clone().&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We relaxed the "only one owner" ownership system rule using shared ownership through &lt;em&gt;Rc&amp;lt;T&amp;gt;&lt;/em&gt; smart pointer.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those solutions have one important thing in common: all three are pass-by-value solutions&lt;sup id="fnref3"&gt;3&lt;/sup&gt;. Another possibility allows us to use the value in another scope while keeping the original ownership intact. We achieve that by using references.&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printable_message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;String&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;"{printable_message}"&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;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sized_message&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="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="n"&gt;sized_message&lt;/span&gt;&lt;span class="nf"&gt;.len&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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;print_message&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;We can think of references as pointers that allow us to access data without taking ownership of it. The difference with a typical C pointer is that we can't make any pointer arithmetic on them, also they are restricted to specific rules. References add two more rules to the ownership system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At any given time, you can have &lt;em&gt;either&lt;/em&gt; one mutable reference &lt;em&gt;or&lt;/em&gt; any number of immutable references.&lt;/li&gt;
&lt;li&gt;References must always be valid.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first rule tells us that references come in two flavors: &lt;em&gt;mutable&lt;/em&gt;, and &lt;em&gt;immutable&lt;/em&gt;. The first ones are denoted by &lt;code&gt;&amp;amp;&lt;/code&gt; and the second ones are denoted by &lt;code&gt;&amp;amp;mut&lt;/code&gt;. We can have any number of immutable references to a value or only one mutable reference to the same value at the same time. That's the reason why the immutable references are often called shared references, and the mutable ones are typically referred to as exclusive references. You can share or mutate, but not both.&lt;/p&gt;

&lt;p&gt;Allowing access to a value in another scope without transferring ownership opens the door to new problems. The ownership system must ensure that the references point exclusively to non-dropped values. That is what the second rule is all about.&lt;/p&gt;

&lt;p&gt;Notice that the &lt;em&gt;print_message()&lt;/em&gt; function was changed to receive a reference of the message, but the &lt;em&gt;print_message_size()&lt;/em&gt; is still taking ownership of the value. Because the &lt;em&gt;print_message()&lt;/em&gt; function doesn't take ownership of the message value, the owner is still the message variable defined in the main function until the second function is called, and that does not break any rule of the ownership system. The code compiles. &lt;/p&gt;

&lt;p&gt;Now let's explore what happens if we invert the call function order:&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;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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;print_message&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// error: borrow of moved value.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compile error changes, telling us that we are trying to take a reference of a value that was moved. A reference is valid when its lifetime is equal to or less than the lifetime of the value that it points to.&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;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;msg_ref&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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;msg_ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// error: `message` does not live long enough&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; 

    &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg_ref&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;In this example, we define a reference variable in the main function scope and initialize it with a reference to a value bonded to an internal scope smaller than the full main function scope. &lt;/p&gt;

&lt;p&gt;When we use that reference after the inner scope has ended, the compiler tells us that the value behind the reference does not live long enough. In other words, the compiler tells us that the reference lifetime is greater than the lifetime of the value that's pointing to. Using a reference that points to a dropped value leads to undefined behavior, so the compiler inhibits that possibility.&lt;/p&gt;

&lt;p&gt;In Rust parlance, when a function takes a reference as a parameter, we say that &lt;em&gt;the function borrows the value&lt;/em&gt;. Besides the ownership moving semantics, &lt;em&gt;borrowing&lt;/em&gt; is the other complementary concept that is prevalent is Rust. The &lt;em&gt;borrow checker&lt;/em&gt; is the part of the compiler that enforces the Rust's ownership and borrowing rules at compile time.&lt;/p&gt;

&lt;p&gt;A full discussion about borrowing must include an explanation of lifetimes, especially generic ones. That topic demands special attention, but since we need to understand the basics of the ownership system first, we’ll leave it for another article.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ownership is the path to the lifetimes. Ownership leads to borrowing. Borrowing leads to references. References lead to lifetimes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Take, Replace (and Swap)
&lt;/h4&gt;

&lt;p&gt;These two options have very specific use cases. In the context of this example, they are more hacks than real solutions, but it is good to know they exist.&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printable_message&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="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;"{printable_message}"&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;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sized_message&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="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="n"&gt;sized_message&lt;/span&gt;&lt;span class="nf"&gt;.len&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="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;print_message&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;mem&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;take&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;mut&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  

   &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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, we use the generic function &lt;em&gt;mem::take&amp;lt;T&amp;gt;(&amp;amp;mut T)&lt;/em&gt;; this function returns the value behind the mutable reference and replaces its original value with the type's default value. If we run this code, the output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Rust!  
0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s not the correct result because &lt;em&gt;print_message()&lt;/em&gt; received the original value, but the value of the variable message was replaced with the default value, which, in the case of the &lt;em&gt;String&lt;/em&gt; type, is an empty string.&lt;/p&gt;

&lt;p&gt;Why does this work in terms of ownership? Because &lt;em&gt;mem::take(&amp;amp;mut T) -&amp;gt; T&lt;/em&gt; receives a mutable reference of &lt;em&gt;T&lt;/em&gt; from which extracts the current value, returns it as an owned value, and, in the middle, replaces the original value with the default value for &lt;em&gt;T&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;As a result, from the perspective of the ownership flow, nothing changes; the &lt;em&gt;message&lt;/em&gt; variable never loses ownership of a value. It's just that the good old &lt;em&gt;mem::take()&lt;/em&gt; swoops in, sneakily swipes the current value from &lt;em&gt;message&lt;/em&gt;'s pocket, and replaces it with a new one of equal &lt;em&gt;weight&lt;/em&gt;. The sleight of hand is so smooth that &lt;em&gt;message&lt;/em&gt; remains blissfully unaware of what transpired, and the ownership rules remain perfectly intact.&lt;/p&gt;

&lt;p&gt;Technically, &lt;code&gt;mem::take(&amp;amp;mut T) -&amp;gt; T&lt;/code&gt; is a special case of &lt;code&gt;mem::replace(&amp;amp;mut T, T) -&amp;gt; T&lt;/code&gt; with the default value of &lt;em&gt;T&lt;/em&gt; as a second argument; in fact, &lt;em&gt;mem::take()&lt;/em&gt; uses &lt;em&gt;mem::replace()&lt;/em&gt; internally.&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;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="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&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;other_message&lt;/span&gt; &lt;span class="o"&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;"Another string value"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// len = 20&lt;/span&gt;

   &lt;span class="nf"&gt;print_message&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;mem&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;replace&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;mut&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other_message&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  

   &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;That’s prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Rust!  
20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just to show the third friend of this group of functions, we’ll see an example of &lt;em&gt;mem::swap(&amp;amp;mut T, &amp;amp;mut T)&lt;/em&gt;, which does exactly what we expect.&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;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="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;message1&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
   &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;message2&lt;/span&gt; &lt;span class="o"&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;"Goodbye Rust!"&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;mem&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;swap&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;mut&lt;/span&gt; &lt;span class="n"&gt;message1&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;mut&lt;/span&gt; &lt;span class="n"&gt;message2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  

   &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message2&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;Printing the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Goodbye Rust!  
Hello Rust!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Redesign to Avoid Ownership Transfer
&lt;/h4&gt;

&lt;p&gt;Another valid option is to rethink how the code manages ownership of values throughout its structure.&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;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printable_message&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="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;"{printable_message}"&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="n"&gt;printable_message&lt;/span&gt;&lt;span class="nf"&gt;.len&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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;Reconsidering the function call order while keeping the final result intact is valid. So, rethink the way that the code is handling ownership of the values through our code should serve us as a trigger to build a better solution, it forces us to find other way to get the same result, if we have the intention to simplify things, in general that lead us to better solution. &lt;/p&gt;

&lt;h4&gt;
  
  
  Inlining
&lt;/h4&gt;

&lt;p&gt;This can be thinking as a special case of the previous option. If we inline the &lt;em&gt;print_message_size()&lt;/em&gt; function call, we end up with 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;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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&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;"{printable_message}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// manual inlined function&lt;/span&gt;

   &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;Removing the function call also eliminates the ownership transfer. While this could resolve the ownership issue, it is not always a practical solution, as it may lead to code duplication and future maintenance challenges.&lt;/p&gt;

&lt;p&gt;Just to mention, Rust has a function attribute called &lt;code&gt;#[inline]&lt;/code&gt;:&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="nd"&gt;#[inline(always)]&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printable_message&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="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;"{printable_message}"&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;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sized_message&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="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="n"&gt;sized_message&lt;/span&gt;&lt;span class="nf"&gt;.len&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;message&lt;/span&gt; &lt;span class="o"&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;"Hello Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;print_message_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;But serves as a compiler hint and not as an actual order that could solve ownership problems. The code is still not compiling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  --&amp;gt; src/bin/inlining.rs:15:24
   |
11 |     let message = String::from("Hello Rust!");
   |         ------- move occurs because `message` has type `String`, which does not implement the `Copy` trait
12 |
13 |     print_message(message);
   |                   ------- value moved here
14 |
15 |     print_message_size(message);
   |                        ^^^^^^^ value used here after move
   |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Related Concepts
&lt;/h2&gt;

&lt;p&gt;After exploring ways to overcome the 'use of moved value' scenario, it's worth learning about some related concepts to complete the ownership picture.&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoid cloning
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;clone()&lt;/em&gt; method creates a deep copy of a value, which in particular does not have anything bad at prior. However, overuse can produce unwanted performance problems and memory bloat, especially if we use it as an easy way to avoid ownership issues.&lt;/p&gt;

&lt;p&gt;That configures the typical Rust tension between cloning and the ownership system, which is why many Rust programmers raise flags against using &lt;em&gt;clone()&lt;/em&gt;. Even so, we must escape from any dogmatic crusade in software design.&lt;/p&gt;

&lt;p&gt;What's really important here is to remember that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Every time the Rust compiler stops you, it’s because a design decision must be taken.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Cloning comes with a cost, and that cost should be carefully considered in the context of our design. It's worth taking the time to ask ourselves a few questions before simply writing &lt;code&gt;.clone()&lt;/code&gt; after naming a value:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can ownership be taken instead?&lt;/li&gt;
&lt;li&gt;Would a reference suffice here?&lt;/li&gt;
&lt;li&gt;Could shared ownership through a smart pointer address the problem?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a rule of thumb, use &lt;code&gt;.clone()&lt;/code&gt; only when there is a specific reason for creating a new copy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Copy Trait
&lt;/h3&gt;

&lt;p&gt;For a moment, let's focus on another simple 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;struct&lt;/span&gt; &lt;span class="n"&gt;Point&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="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&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;print_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Point&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="n"&gt;p&lt;/span&gt;&lt;span class="py"&gt;.x&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;print_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Point&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="n"&gt;p&lt;/span&gt;&lt;span class="py"&gt;.y&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;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Point&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="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nf"&gt;print_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;print_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// error: use of moved value: `p1`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is similar to the previous example in the sense that we are getting the same error from the compiler. But the kind of the types that our &lt;em&gt;Point&lt;/em&gt; struct is based on allows us to use another way of solving the ownership issue.&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="nd"&gt;#[derive(Copy,&lt;/span&gt; &lt;span class="nd"&gt;Clone)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Point&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="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&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;Because our struct uses only copy values, we can derive the Copy trait for Point. Copy values in Rust refer to values of types that implement the Copy trait. This trait allows for the duplication of values without transferring ownership. The Copy trait is automatically implemented for simple, fixed-size types, like integers, floating-point, booleans, and character types.&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;Copy&lt;/code&gt; trait for custom types involves design decisions similar to those made with the &lt;em&gt;clone()&lt;/em&gt; method use. The key difference lies in their purposes: &lt;code&gt;Copy&lt;/code&gt; is a marker trait that signals to the compiler that a type's value can be duplicated by simply copying its bits in memory. In contrast, the &lt;code&gt;Clone&lt;/code&gt; trait defines an explicit method for creating a copy of a value. For types implementing &lt;code&gt;Copy&lt;/code&gt;, values are automatically duplicated instead of being moved when passed to a function or assigned to another variable. In this sense, the &lt;code&gt;Copy&lt;/code&gt; trait represents a controlled exception to Rust's default move semantics in the ownership system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interior Mutability
&lt;/h3&gt;

&lt;p&gt;Some smart pointers (&lt;em&gt;Cell, RefCell, RwLock, Mutex&lt;/em&gt;) allows the use of the pattern known as &lt;em&gt;interior mutability&lt;/em&gt;. Interior mutability allows us to mutate a value through a shared reference. I can almost hear you saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Wait! What? We've seen that there are two types of references: mutable and shared. We can't mix them—we can either share or mutate through them, but not both!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My answer is, &lt;em&gt;'Yes, you are right,'&lt;/em&gt; but this pattern is an exception to the ownership rules we've seen before—in more ways than one.&lt;/p&gt;

&lt;p&gt;First, it clearly allows something that is otherwise forbidden:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At any given time, you can have &lt;em&gt;either&lt;/em&gt; one mutable reference &lt;em&gt;or&lt;/em&gt; any number of immutable references.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this approach, we can have multiple shared (immutable) references while still allowing mutation of the value through any of them. In other words, it violates the principle of inherited mutability&lt;/p&gt;

&lt;p&gt;Second, inherited mutability is enforced at compile time by (of course) the compiler. However, interior mutability relies on run time checks for safety. This means that the this family of containers enforces the necessary borrowing rules dynamically, allowing mutation through shared references while maintaining safety within their defined constraints. However, unlike inherited mutability, which ensures safety at compile time, this approach introduces a run time cost, as the rules are enforced programmatically during execution.&lt;/p&gt;

&lt;p&gt;Third, because the rules of interior mutability are checked at run time, violations result in run time errors. This means that, for the interior mutability portion of a program, the occurrence of memory management issues depends on the program’s logic and the execution paths determined by run time conditions. These conditions are outside the compiler's scope, preventing it from catching such issues. This breaks Rust’s promise of catching these types of problems at compile time.&lt;/p&gt;

&lt;p&gt;Why does interior mutability even exist if it comes with all those drawbacks? The Rust compiler is quite conservative about what constitutes a valid program. In terms of memory safety, it prefers to be strict, even at the cost of having false positive classifications of unsafe programs. Using group theory terminology, we could say that the set of memory-safe programs is a superset of the programs allowed by the Rust compiler. This leaves a whole subset of valid programs that cannot be represented without the interior mutability pattern.&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;cell&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Cell&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;Counter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Cell&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// instead of: "count: u32"&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;Counter&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;new&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;Counter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Counter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Cell&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="mi"&gt;0&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;increment&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;// self.count += 1; // cannot assign to `self.count`, which is behind a `&amp;amp;` reference&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.count&lt;/span&gt;&lt;span class="nf"&gt;.set&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;.count&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&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="nf"&gt;get&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="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;self&lt;/span&gt;&lt;span class="py"&gt;.count&lt;/span&gt;&lt;span class="nf"&gt;.get&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="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Counter&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Initial count: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="nf"&gt;.increment&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;"Incremented count: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="nf"&gt;.get&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;In the example above the interior mutability pattern allow us to expose an immutable interface of the counter but and the same time encapsulating the mutation of the counter in the increment method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Based on simple examples we learnt the rules of the ownership system, and we went through several ways to resolve ownership issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can take and return ownership of values without cutting the ownership flow.&lt;/li&gt;
&lt;li&gt;We can pass a copy of the value avoiding the ownership move.&lt;/li&gt;
&lt;li&gt;We can share ownership using reference counting.&lt;/li&gt;
&lt;li&gt;We can give access to values without renounce to ownership via borrowing using references.&lt;/li&gt;
&lt;li&gt;And we can refactor the code to remove the ownership transfer rethinking the function call order or manually inlining code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Witch option we should pick? Well, like many interesting answers in life, it begins with the word: depends. There is no magic rule to follow any time, because the selected option would depends on the problem you have into hands. &lt;/p&gt;

&lt;p&gt;As general guidance immutable and mutable references are usually the most flexible option, because allow access to a value without transferring ownership, avoiding the need for cloning or moving data. One typical exception are the constructors where taking ownership of the values is usually the best option if we want to keep the ownership flows simple and understandable.&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;struct&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Person&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;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// instead of: fn new(name: &amp;amp;String) -&amp;gt; Person&lt;/span&gt;
        &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// instead of: name: name.clone()&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;get_name&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// instead of: fn get_name(&amp;amp;self) -&amp;gt; String&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="py"&gt;.name&lt;/span&gt; &lt;span class="c1"&gt;// instead of: self.name.clone()&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;The main advantage to make an interface that returns references is we left the callers and the client code decide if the reference will suffice or if they need to clone the value instead.&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;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;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Person&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="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;"John"&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="nf"&gt;.get_name&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;other_john&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Person&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="n"&gt;name&lt;/span&gt;&lt;span class="nf"&gt;.clone&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="n"&gt;person&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other_john&lt;/span&gt;&lt;span class="py"&gt;.name&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;For other more complex interactions where we need to share some data or custom type across the program logic we should consider the &lt;em&gt;Rc&amp;lt;T&amp;gt;&lt;/em&gt; approach. And if we are follow a more functional approach where immutability is central, make interfaces that returns  cloned owned value instead of references would help.&lt;/p&gt;

&lt;p&gt;We must remember that, from a static analysis perspective, memory management in Rust is enforced syntactically&lt;sup id="fnref4"&gt;4&lt;/sup&gt;. The compiler relies solely on the code’s structure and ownership rules (like moves, borrows, and lifetimes) to statically validate memory safety, even though memory operations are inherently dynamic at runtime.&lt;/p&gt;

&lt;p&gt;This strict compile-time checking is a key contributor to Rust's learning curve. We're not just learning ideas like ownership and borrowing—we have to write code in a way that makes the borrow checker happy, which often requires adjusting code that seems correct or restructuring logic to meet the compiler’s expectations. While this can feel restrictive, it reflects Rust’s core trade-off: rejecting some valid patterns upfront to eliminate memory vulnerabilities entirely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Words
&lt;/h2&gt;

&lt;p&gt;Starting with a simple 'use of moved value' scenario, we explored various ownership-related concepts and techniques. We also examined ways to relax or even defy the static rules with dynamic replacements. I'm confident that we've now built a practical understanding of how the ownership system works, which is essential for tackling more complex scenarios. Stay tuned!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Licensed under &lt;a href="https://creativecommons.org/licenses/by-sa/4.0/" rel="noopener noreferrer"&gt;CC BY-SA 4.0&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;References:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=SWwTD2neodE" rel="noopener noreferrer"&gt;YouTube: 5 Deadly Rust Anti-Patterns to Avoid.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.manning.com/books/idiomatic-rust" rel="noopener noreferrer"&gt;Book: Idiomatic Rust by Brenden Matthews - Chapters 1 and 2.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://rust-for-rustaceans.com/" rel="noopener noreferrer"&gt;Book: Rust for Rustaceans by Jon Gjengset - Chapters 1 and 3.&lt;/a&gt;&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;There are two exceptions to this: the use of unsafe code to bypass the mutability restrictions and the use of shareable mutable containers Cell, RefCell, and OnceCell, which allows us to mutate the data inside an immutable container. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Internally, &lt;em&gt;Rc&lt;/em&gt; uses &lt;em&gt;PhantomData&lt;/em&gt;, a zero-cost abstraction, to represent ownership without storing an actual instance of T. Including &lt;em&gt;PhantomData&lt;/em&gt; allows the compiler to track ownership and enforce type relationships at compile time without spend more memory.  ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;The &lt;em&gt;Rc&lt;/em&gt; solution is more subtle because it implies the pass-by-value of the smart pointer itself instead of the wrapped value. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;While Rust’s memory management is enforced through code structure (e.g., moves, borrows, and lifetimes), ownership itself is a semantic concept. Rust’s compile-time enforcement is rooted in memory safety semantics, even though it relies on syntactic patterns to express those rules. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>rust</category>
      <category>ownership</category>
      <category>learning</category>
      <category>programming</category>
    </item>
    <item>
      <title>Smalltalk meets Forth: Episode I</title>
      <dc:creator>Jorge Luis Perez</dc:creator>
      <pubDate>Thu, 06 Feb 2025 00:38:42 +0000</pubDate>
      <link>https://dev.to/jolisper/smalltalk-meets-forth-episode-i-532p</link>
      <guid>https://dev.to/jolisper/smalltalk-meets-forth-episode-i-532p</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F36z88ju8u91zk9swfudr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F36z88ju8u91zk9swfudr.png" alt="Illustration from Thinking Forth by Leo Brodie, showing two fairies dressed as middle-aged programmers, humorously discussing Forth as both a high-level and low-level language. Image by the author." width="800" height="646"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The idea is simple: build a Forth interpreter in Smalltalk in a series of posts and learn some Forth and Smalltalk in the process.&lt;/p&gt;

&lt;p&gt;For this first session we would like to end with a fully working Forth REPL with at least some of the basic math words implemented. Also, a pretty basic error handling would be good to have from the beginning.&lt;/p&gt;

&lt;p&gt;There are several Smalltalk flavors to choose from, but I share Cuis-Smalltalk’s love for small, simple, and understandable things. So, ‘Cuis, I choose you!’ 🐹&lt;/p&gt;

&lt;p&gt;Another tool for the journey is the Forth standard. I aim to use its references, jargon, and overall organization to keep the interpreter as close to the standard as possible, while still allowing room for more creative or implementation-specific words.&lt;/p&gt;

&lt;p&gt;One advantage we have is there are many Forth implementations out there, so we can use them as a reference of the word's behaviors and outputs on the REPL, &lt;a href="https://gforth.org/" rel="noopener noreferrer"&gt;GForth&lt;/a&gt; and &lt;a href="https://www.forth.com/swiftforth/" rel="noopener noreferrer"&gt;SwithForth&lt;/a&gt; would be a good source of inspiration.&lt;/p&gt;

&lt;p&gt;I prefer to introduce the Forth concepts as we need it, but a little introduction will put us all on the same page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Forth
&lt;/h2&gt;

&lt;p&gt;Forth is a stack-based, concatenative programming language and environment that is designed for simplicity and extensibility. Stack-based means that Forth uses a stack to hold data and control flow, operations are performed by pushing and popping values from the stack. We can think in Forth as a thin abstraction over a stack machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqf7gnhblju2hdkzwcew.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqf7gnhblju2hdkzwcew.gif" alt="Forth stack base example." width="974" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Concatenative means that programs are composed of sequences of words that can be combined together. Each word can be defined in terms of other words, allowing for a high degree of modularity and reuse.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Forth typically combines a compiler with an integrated command shell, where the user interacts via subroutines called words. Words can be defined, tested, redefined, and debugged without recompiling or restarting the whole program. All syntactic elements, including variables, operators, and control flow, are defined as words. A stack is used to pass parameters between words, leading to a Reverse Polish Notation style.&lt;/p&gt;

&lt;p&gt;From Wikipedia&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Similar to Smalltalk, which has the strong metaphor ‘Everything is an object’, Forth embodies the idea that ‘Everything is a word’.&lt;/p&gt;

&lt;h2&gt;
  
  
  The REPL
&lt;/h2&gt;

&lt;p&gt;One thing I really enjoy about Smalltalk is its level of interactivity; you have inspectors, explorers, and debuggers that allow you to have a programming conversation with the environment. I want to keep the journey as conversational as possible, so we need to start by creating a simple REPL where we can experiment with our Forth implementation as we build it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Folmzdwba19jegyktxiyi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Folmzdwba19jegyktxiyi.png" alt="Text editor in menu." width="621" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The good news is that the Cuis environment already has a similar editor we can use as a reference to understand how it’s built. The Text Editor offers more functionality than a REPL, so we can think of a REPL as a limited version of a Text Editor, with some features intentionally restricted. It might be a bit much, but the structure and most of the behavior are useful for our case. I think it could be a good place to start.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdr1cbu73f8p8noeykuu4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdr1cbu73f8p8noeykuu4.png" alt="Text editor - Explore option in menu." width="800" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Exploring the Text Editor using the halos, we learn that the main Morphic structure of the Text Editor is:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;SystemWindow-&amp;gt; LayoutMorph -&amp;gt; TextModelMorph -&amp;gt; InnerTextMorph&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The halos in Smalltalk offer an interactive way to explore graphical objects. By clicking on halos, you can inspect graphical object properties and relationships, providing a hands-on approach to understanding the system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gxpkl61wbypf658hila.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gxpkl61wbypf658hila.png" alt="Text Editor - Exploring submorphs." width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After spending some time tinkering and winging it with browsers, explorers and inspectors, I found that the behavior we need to specialize is mostly within the InnerTextMorph. Also, we need to make a little adjustment to TextModelMorph to use the specialized InnerTextMorph.&lt;/p&gt;

&lt;p&gt;I create ForthTextModelMorph and ForthInnerTextMorph classes which are subclasses of TextModelMorph and InnerTextMorph respectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;TextModelMorph&lt;/span&gt; &lt;span class="nf"&gt;subclass:&lt;/span&gt; &lt;span class="ss"&gt;#ForthTextModelMorph&lt;/span&gt;
   &lt;span class="nf"&gt;instanceVariableNames:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
   &lt;span class="nf"&gt;classVariableNames:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
   &lt;span class="nf"&gt;poolDictionaries:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
   &lt;span class="nf"&gt;category:&lt;/span&gt; &lt;span class="s"&gt;'CuiForth-Morph'&lt;/span&gt;

&lt;span class="nf"&gt;InnerTextMorph&lt;/span&gt; &lt;span class="nf"&gt;subclass:&lt;/span&gt; &lt;span class="ss"&gt;#ForthInnerTextMorph&lt;/span&gt;
   &lt;span class="nf"&gt;instanceVariableNames:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
   &lt;span class="nf"&gt;classVariableNames:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
   &lt;span class="nf"&gt;poolDictionaries:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
   &lt;span class="nf"&gt;category:&lt;/span&gt; &lt;span class="s"&gt;'CuiForth-Morph'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is necessary to set the inner morph class for ForthTextModelMorph.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthModelMorph&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;innerMorphClass&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="nc"&gt;ForthInnerTextMorph&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step determines that the inner text handling behavior of ForthModelMorph will be managed by ForthInnerTextMorph.&lt;/p&gt;

&lt;p&gt;To integrate the entire morph structure, we introduce the ForthREPL class. This is the primary class responsible for running the REPL, initializing the system, and holding the Morphic structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;subclass:&lt;/span&gt; &lt;span class="ss"&gt;#ForthREPL&lt;/span&gt;
    &lt;span class="nf"&gt;instanceVariableNames:&lt;/span&gt; &lt;span class="s"&gt;'window'&lt;/span&gt;
    &lt;span class="nf"&gt;classVariableNames:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
    &lt;span class="nf"&gt;poolDictionaries:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
&lt;span class="err"&gt;    category:&lt;/span&gt; &lt;span class="s"&gt;'CuiForth-Window'&lt;/span&gt;

&lt;span class="nf"&gt;ForthREPL&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;initialize&lt;/span&gt;
    &lt;span class="nf"&gt;|&lt;/span&gt; &lt;span class="nv"&gt;forthTextModelMorph&lt;/span&gt; &lt;span class="nf"&gt;forthTextModel&lt;/span&gt; &lt;span class="nf"&gt;|&lt;/span&gt;
    &lt;span class="bp"&gt;super&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="nv"&gt;forthTextModel&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nc"&gt;ForthTextModel&lt;/span&gt; &lt;span class="nb"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="nv"&gt;forthTextModelMorph&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nc"&gt;ForthTextModelMorph&lt;/span&gt; &lt;span class="nb"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

 &lt;span class="c"&gt;"Connnect the text models"&lt;/span&gt;
 &lt;span class="nv"&gt;forthTextModelMorph&lt;/span&gt; &lt;span class="nf"&gt;model:&lt;/span&gt; &lt;span class="nv"&gt;forthTextModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

 &lt;span class="c"&gt;"The Forth model morph will use all the available width and heigth"&lt;/span&gt;
 &lt;span class="nv"&gt;forthTextModelMorph&lt;/span&gt; &lt;span class="nf"&gt;layoutSpec&lt;/span&gt;
     &lt;span class="nf"&gt;proportionalWidth:&lt;/span&gt; &lt;span class="m"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nf"&gt;proportionalHeight:&lt;/span&gt; &lt;span class="m"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

 &lt;span class="c"&gt;"Main window configuration"&lt;/span&gt;
 &lt;span class="nv"&gt;window&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nc"&gt;SystemWindow&lt;/span&gt; &lt;span class="nb"&gt;new&lt;/span&gt;
     &lt;span class="nf"&gt;setLabel:&lt;/span&gt; &lt;span class="s"&gt;'CuiForth REPL'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nf"&gt;model:&lt;/span&gt; &lt;span class="nv"&gt;forthTextModel&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nf"&gt;addMorph:&lt;/span&gt; &lt;span class="nv"&gt;forthTextModelMorph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The structure ends like this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;SystemWindow -&amp;gt; LayoutMorph -&amp;gt; ForthModelMorph -&amp;gt; ForthInnerTextMorph.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Another little method to open the REPL could be handy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthREPL&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;open&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;window&lt;/span&gt; &lt;span class="nf"&gt;openInWorld&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ForthInnerTextMorph is where we need to add our keystroke handle logic. The most important thing is to handle the return key to process the words at the current line. The rest of the logic is to customize the text editor to behave like a REPL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthInnerTextMorph&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;keyStroke:&lt;/span&gt; &lt;span class="nv"&gt;aKeyboardEvent&lt;/span&gt;
    &lt;span class="nv"&gt;aKeyboardEvent&lt;/span&gt; &lt;span class="nf"&gt;isReturnKey&lt;/span&gt; &lt;span class="nb"&gt;ifTrue:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nv"&gt;editor&lt;/span&gt; &lt;span class="nf"&gt;processWords&lt;/span&gt;
    &lt;span class="p"&gt;].&lt;/span&gt;

    &lt;span class="c"&gt;"We don't want backspace to go back to the previous line"&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;ignoreBackspaceWhenLineIsEmpty:&lt;/span&gt; &lt;span class="nv"&gt;aKeyboardEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

    &lt;span class="c"&gt;"For now, ignore the up and down arrow keys;
       they could serve later to navigate previous commands"&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;ignoreUpArrowKey:&lt;/span&gt; &lt;span class="nv"&gt;aKeyboardEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;ignoreDownArrowKey:&lt;/span&gt; &lt;span class="nv"&gt;aKeyboardEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

    &lt;span class="c"&gt;"Let the normal keystroke processing continue"&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="bp"&gt;super&lt;/span&gt; &lt;span class="nf"&gt;keyStroke:&lt;/span&gt; &lt;span class="nv"&gt;aKeyboardEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that for word processing ForthInnerTextMorph delegates this responsibility in its internal collaborator editor (ForthEditor). This separation of concerns might allow us to reutilize the editor with other Morphic configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;TextEditor&lt;/span&gt; &lt;span class="nf"&gt;subclass:&lt;/span&gt; &lt;span class="ss"&gt;#ForthTextEditor&lt;/span&gt;
    &lt;span class="nf"&gt;instanceVariableNames:&lt;/span&gt; &lt;span class="s"&gt;'extractedWords interpreter'&lt;/span&gt;
    &lt;span class="nf"&gt;classVariableNames:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
    &lt;span class="nf"&gt;poolDictionaries:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
&lt;span class="err"&gt;    category:&lt;/span&gt; &lt;span class="s"&gt;'CuiForth-Text'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the editor knows the interpreter. The class of the text editor is configured in the text model class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;TextModel&lt;/span&gt; &lt;span class="nf"&gt;subclass:&lt;/span&gt; &lt;span class="ss"&gt;#ForthTextModel&lt;/span&gt;
    &lt;span class="nf"&gt;instanceVariableNames:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
    &lt;span class="nf"&gt;classVariableNames:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
    &lt;span class="nf"&gt;poolDictionaries:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
    &lt;span class="nf"&gt;category:&lt;/span&gt; &lt;span class="s"&gt;'CuiForth-Text'&lt;/span&gt;

&lt;span class="nf"&gt;ForthTextModel&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;editorClass&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="nc"&gt;ForthTextEditor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main responsibility of the ForthEditor is handled by its processWords message. This method is designed to extract the words from the current line (after the return key is pressed), pass those words to the interpreter, and display the interpreter’s output to the user. For now, a placeholder implementation that always responds with ‘ok’ for any input will suffice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthEditor&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;processWords&lt;/span&gt;
    &lt;span class="nf"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;addString:&lt;/span&gt; &lt;span class="s"&gt;' ok'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s open the REPL and do some manual testing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy299lkfs0auqmkfgcqrr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy299lkfs0auqmkfgcqrr.png" alt="Forth REPL open." width="351" height="142"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20lukp97y54hj44tzv16.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20lukp97y54hj44tzv16.gif" alt="Forth REPL testing." width="506" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Until now, I've used a more exploratory programming style, checking changes in the Workspace. For the remaining features, I followed a mostly TDD approach, but I don’t think that’s the best flow for the article — let me continue with a more descriptive tone.&lt;/p&gt;

&lt;p&gt;With the REPL ready we can start to implement Forth features.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Note about the REPL
&lt;/h3&gt;

&lt;p&gt;A REPL is the usual way to interact with Forth, giving quick feedback as you type. But in Smalltalk, the go-to tool for playing with code is the Workspace. It’s like a live text editor where you can write, run, and tweak code snippets on the spot. We’ll explore that possibility for our Forth interpreter later.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Stack
&lt;/h2&gt;

&lt;p&gt;As we said, Forth is stack-based. In general, it has two main stacks: the data stack and the return stack. The former holds data items, and the latter manages return addresses for control flow. Let’s start with the former.&lt;/p&gt;

&lt;p&gt;We only need a classic stack, with a pretty printString method to use later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;subclass:&lt;/span&gt; &lt;span class="ss"&gt;#ForthDataStack&lt;/span&gt;
    &lt;span class="nf"&gt;instanceVariableNames:&lt;/span&gt; &lt;span class="s"&gt;'collection'&lt;/span&gt;
    &lt;span class="nf"&gt;classVariableNames:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
    &lt;span class="nf"&gt;poolDictionaries:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
    &lt;span class="nf"&gt;category:&lt;/span&gt; &lt;span class="s"&gt;'CuiForth-Interpreter'&lt;/span&gt;

&lt;span class="nf"&gt;ForthDataStack&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;initialize&lt;/span&gt;
    &lt;span class="nf"&gt;collection&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nc"&gt;OrderedCollection&lt;/span&gt; &lt;span class="nb"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="nc"&gt;ForthDataStack&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;pop&lt;/span&gt;
    &lt;span class="nf"&gt;collection&lt;/span&gt; &lt;span class="nf"&gt;ifEmpty:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nc"&gt;StackUnderflow&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;].&lt;/span&gt;
    &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="nv"&gt;collection&lt;/span&gt; &lt;span class="nf"&gt;removeLast&lt;/span&gt;

&lt;span class="nf"&gt;ForthDataStack&amp;gt;&amp;gt;push:&lt;/span&gt; &lt;span class="nv"&gt;anElement&lt;/span&gt;
    &lt;span class="nv"&gt;collection&lt;/span&gt; &lt;span class="nf"&gt;addLast:&lt;/span&gt; &lt;span class="nv"&gt;anElement&lt;/span&gt;

&lt;span class="nf"&gt;ForthDataStack&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;printString&lt;/span&gt;
    &lt;span class="nf"&gt;|&lt;/span&gt; &lt;span class="nv"&gt;count&lt;/span&gt; &lt;span class="nf"&gt;items&lt;/span&gt; &lt;span class="nf"&gt;|&lt;/span&gt;
    &lt;span class="nv"&gt;count&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;collection&lt;/span&gt; &lt;span class="nf"&gt;size&lt;/span&gt; &lt;span class="nf"&gt;asString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="nv"&gt;items&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;collection&lt;/span&gt; &lt;span class="nf"&gt;inject:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt; &lt;span class="nf"&gt;into:&lt;/span&gt;
&lt;span class="err"&gt;        &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;item&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nv"&gt;acc&lt;/span&gt;&lt;span class="nf"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;item&lt;/span&gt; &lt;span class="nf"&gt;printString,&lt;/span&gt; &lt;span class="s"&gt;' '&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="s"&gt;'&amp;lt;'&lt;/span&gt;&lt;span class="nf"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;count&lt;/span&gt;&lt;span class="nf"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'&amp;gt; '&lt;/span&gt;&lt;span class="nf"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most interesting thing here is that the stack signals a StackUnderflow if we try to pop an element when the collection is empty. In Forth, all parameters are passed via the stack, with words operating by popping and pushing data to and from it. If a word needs a parameter that isn’t on the stack, we definitely want to know.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Interpreter
&lt;/h2&gt;

&lt;p&gt;Finally, we arrive at the core of the project: the Forth interpreter. First, we’ll discuss the general logic, followed by some interesting implementation details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;subclass:&lt;/span&gt; &lt;span class="ss"&gt;#ForthInterpreter&lt;/span&gt;
    &lt;span class="nf"&gt;instanceVariableNames:&lt;/span&gt; &lt;span class="s"&gt;'dataStack display'&lt;/span&gt;
    &lt;span class="nf"&gt;classVariableNames:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
    &lt;span class="nf"&gt;poolDictionaries:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;
&lt;span class="err"&gt;    category:&lt;/span&gt; &lt;span class="s"&gt;'CuiForth-Interpreter'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dataStack internal collaborator is self-explanatory: it holds an instance of the ForthDataStack, as discussed earlier. The display is where printable messages generated during the interpretation process are stored. The editor object will use this to show messages to the REPL user.&lt;/p&gt;

&lt;p&gt;The interpreter is responsible for implementing all Forth words. To organize them, I’ve used the standard group names as message categories. The naming conventions follow the standard as the main reference.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Standard-Group-Words.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Standard-Group-Words.png" title="Forth - Standard Group Words" alt="Forth - Standard Group Words" width="336" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Forth standard categories into messages categories:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pj6cdlxprp7bmjfbcxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pj6cdlxprp7bmjfbcxg.png" alt="Forth Interpreter - Message categories." width="430" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The main message of the interpreter is execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthIntepreter&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;execute:&lt;/span&gt; &lt;span class="nv"&gt;textWords&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="nv"&gt; tokens &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;

    &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;clearDisplay&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

    &lt;span class="nv"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;tokenize:&lt;/span&gt; &lt;span class="nv"&gt;textWords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;tokens&lt;/span&gt; &lt;span class="nf"&gt;do:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
       &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="nf"&gt;execute:&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
       &lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;on:&lt;/span&gt; &lt;span class="nv"&gt;ForthError&lt;/span&gt;
       &lt;span class="err"&gt;    do:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;error&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;display:&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt; &lt;span class="nf"&gt;description&lt;/span&gt; &lt;span class="p"&gt;]].&lt;/span&gt;

    &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;displayOk&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method receives a line of text (the words), tokenizes it, and then processes each token one by one. Tokenization in Forth refers to splitting the line of text into individual words (or tokens), which are separated by whitespaces. Each token is treated as an atomic command or operand, and the interpreter executes these in sequence. This straightforward process is key to how Forth operates, where each word in the code is treated as a command, making the interpreter’s design simpler and more direct.&lt;/p&gt;

&lt;p&gt;Forth’s tokenization typically does not involve complex parsing rules; rather, it focuses on identifying individual, space-separated sequences of characters that represent commands, variables, or numbers: words in Forth parlance. In Forth everything is a word.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthInterpreter&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;tokenize:&lt;/span&gt; &lt;span class="nv"&gt;aString&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="nv"&gt; textWords &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;
    &lt;span class="nv"&gt;textWords&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;aString&lt;/span&gt; &lt;span class="nf"&gt;substrings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

    &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="nv"&gt;textWords&lt;/span&gt; &lt;span class="nf"&gt;collect:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;textWord&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
&lt;span class="err"&gt;        &lt;/span&gt;&lt;span class="nc"&gt;ForthToken&lt;/span&gt; &lt;span class="nf"&gt;newFrom:&lt;/span&gt; &lt;span class="nv"&gt;textWord&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method takes a string, splits it into words using substrings, and turns each word into a ForthToken. The ForthToken class is the base class for all tokens, and it knows how to create the right type of token from each word.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Token-hierarchy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Token-hierarchy.png" title="ForthToken hierarchy" alt="ForthToken hierarchy" width="340" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To do this, it goes through its subclasses until it finds one that matches the string. Each subclass knows how to check if it matches a given string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthToken&lt;/span&gt; &lt;span class="nf"&gt;class&amp;gt;&amp;gt;newFrom:&lt;/span&gt; &lt;span class="nv"&gt;aStringWord&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;tokenTypes&lt;/span&gt; &lt;span class="nf"&gt;do:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;tokenType&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
        &lt;span class="nv"&gt;tokenType&lt;/span&gt; &lt;span class="nf"&gt;ifMatch:&lt;/span&gt; &lt;span class="nv"&gt;aStringWord&lt;/span&gt; &lt;span class="nf"&gt;eval:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;].&lt;/span&gt;

&lt;span class="nc"&gt;ForthToken&lt;/span&gt; &lt;span class="nf"&gt;class&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;tokenTypes&lt;/span&gt;
    &lt;span class="c"&gt;"Return all token types in tokenization order"&lt;/span&gt;
    &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;subclasses&lt;/span&gt; &lt;span class="nf"&gt;sort:&lt;/span&gt;
&lt;span class="err"&gt;        &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;b&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nv"&gt;a&lt;/span&gt; &lt;span class="nf"&gt;tokenizeOrder&lt;/span&gt; &lt;span class="nf"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt; &lt;span class="nf"&gt;tokenizeOrder&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After tokenization, the interpreter executes each token one by one. One important thing to note in ForthInterpreter&amp;gt;&amp;gt;execute: is the double dispatch happening in this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthIntepreter&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;execute:&lt;/span&gt; &lt;span class="nv"&gt;textWords&lt;/span&gt;
    &lt;span class="err"&gt;...&lt;/span&gt;
    &lt;span class="nv"&gt;tokens&lt;/span&gt; &lt;span class="nf"&gt;do:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
    &lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="nf"&gt;execute:&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;"&amp;lt;- Double dispatch"&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;..&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this design, each token knows how to execute itself, sending the appropriate message to the interpreter, which holds the implementation for each word. The tokens inherit the following method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthToken&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;execute:&lt;/span&gt; &lt;span class="nv"&gt;aForthInterpreter&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;aForthInterpreter&lt;/span&gt; &lt;span class="nf"&gt;perform:&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The perform: message in Smalltalk is a powerful metaprogramming feature that allows you to send a message to an object dynamically at runtime. It takes a symbol as its parameter and sends the corresponding message to the receiver. This is especially useful in cases like our Forth interpreter, where the message to be send is determined at runtime.&lt;/p&gt;

&lt;p&gt;In the context of our interpreter, the value of a token is its representation as a Smalltalk symbol. For example, a token value #plus represent the addition operation in Forth. The exact message that will be sent is based on the token’s symbolic value, determined during tokenization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;MathSymbolToken&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;newFrom:&lt;/span&gt; &lt;span class="nv"&gt;aString&lt;/span&gt;
    &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nb"&gt;new&lt;/span&gt; 
&lt;span class="err"&gt;        value:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;mathSymbols&lt;/span&gt; &lt;span class="nf"&gt;at:&lt;/span&gt; &lt;span class="nv"&gt;aString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;asSymbol&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some tokens include a dictionary that maps strings to their corresponding symbols. These symbols are then converted into messages sent to the interpreter using the perform: message, as shown earlier. For example, in the MathSymbolsToken class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;MathSymbolToken&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;mathSymbols&lt;/span&gt;
&lt;span class="err"&gt;   &lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="nc"&gt;Dictionary&lt;/span&gt; &lt;span class="nf"&gt;newFrom:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;       &lt;/span&gt;&lt;span class="s"&gt;'+'&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;#plus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="err"&gt;       &lt;/span&gt;&lt;span class="s"&gt;'-'&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;#minus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="err"&gt;       &lt;/span&gt;&lt;span class="s"&gt;'*'&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;#star&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="err"&gt;       &lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;#slash&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="err"&gt;   &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We don’t want to reevaluate the dictionary every time. By surrounding the expression with backticks, we create a compound literal. This approach ensures all components of the compound literal are resolved when the code is compiled, rather than at runtime, acting like a constant. All components of a compound literal must be known when the code is compiled.&lt;/p&gt;

&lt;p&gt;Looking back to the tokenization part of the process, there is an interesting thing happening in ForthToken class&amp;gt;&amp;gt;newFrom: method, there is no return except for the one in the block passed as argument to ifMatch:eval message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthToken&lt;/span&gt; &lt;span class="nf"&gt;class&amp;gt;&amp;gt;newFrom:&lt;/span&gt; &lt;span class="nv"&gt;aStringWord&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;tokenTypes&lt;/span&gt; &lt;span class="nf"&gt;do:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;tokenType&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
&lt;span class="err"&gt;        &lt;/span&gt;&lt;span class="nv"&gt;tokenType&lt;/span&gt; &lt;span class="nf"&gt;ifMatch:&lt;/span&gt; &lt;span class="nv"&gt;aStringWord&lt;/span&gt; &lt;span class="nf"&gt;eval:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;"&amp;lt;--"&lt;/span&gt; &lt;span class="p"&gt;].&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Blocks in Smalltalk are full closures. This means they capture not only the values of variables from their defining context but also the entire execution environment, including the return point of the method where they are defined. Additionally, blocks support lazy evaluation, meaning the execution is deferred until explicitly evaluated. What makes them full closures is their ability to maintain this environment even when the block is executed in a different scope, which allows for non-local returns and the preservation of local state across different contexts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;MathSymbolToken&lt;/span&gt; &lt;span class="nf"&gt;class&amp;gt;&amp;gt;ifMatch:&lt;/span&gt; &lt;span class="nv"&gt;aString&lt;/span&gt; &lt;span class="nf"&gt;eval:&lt;/span&gt; &lt;span class="nv"&gt;aTokenBlock&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;mathSymbols&lt;/span&gt; &lt;span class="nf"&gt;at:&lt;/span&gt; &lt;span class="nv"&gt;aString&lt;/span&gt; &lt;span class="nf"&gt;ifPresent:&lt;/span&gt;
&lt;span class="err"&gt;        &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;symbol&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nv"&gt;aTokenBlock&lt;/span&gt; &lt;span class="nf"&gt;value:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;newFrom:&lt;/span&gt; &lt;span class="nv"&gt;symbol&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;In the case of ForthToken&amp;gt;&amp;gt;newFrom:, the ^ token inside the block allows the ifMath:eval method to terminate immediately and return the desired token as soon as a match is found. When the aTokenBlock block is evaluated with the matched token passed as an argument via the value: message in the ifMatched:eval: context, the ForthToken class&amp;gt;&amp;gt;newFrom: completes and returns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthToken&lt;/span&gt; &lt;span class="nf"&gt;class&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;newFrom:&lt;/span&gt; &lt;span class="err"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;...&lt;/span&gt;
    &lt;span class="nv"&gt;tokenType&lt;/span&gt; &lt;span class="nf"&gt;ifMatched:&lt;/span&gt;&lt;span class="err"&gt;eval:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="err"&gt;...&lt;/span&gt;
    &lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;aTokenBlock&lt;/span&gt; &lt;span class="nf"&gt;eval:&lt;/span&gt; &lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="c"&gt;"returns from newFrom:"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Blocks are central to Smalltalk’s language design. They enable message-based control flow (without the need for if statements), iterators (such as select:, collect: and others), lazy evaluation, non-local returns, error handling, and programming techniques like continuation passing style.&lt;/p&gt;

&lt;p&gt;Now we have covered the main functionality of the interpreter, and some interesting details. Let’s check the word’s implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Math
&lt;/h2&gt;

&lt;p&gt;To make basic arithmetic work in our interpreter, the first thing to handle is numbers — positive and negative integers, to be specific. Earlier, we saw how tokens are recognized and how they handle their own execution. The default behavior for all tokens looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthToken&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;execute:&lt;/span&gt; &lt;span class="nv"&gt;aForthInterpreter&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;aForthInterpreter&lt;/span&gt; &lt;span class="nf"&gt;perform:&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s okay for most words in Forth, but numbers require specialized behavior:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;NumberToken&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;execute:&lt;/span&gt; &lt;span class="nv"&gt;aForthInterpreter&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;aForthInterpreter&lt;/span&gt; &lt;span class="nf"&gt;pushToDataStack:&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Number words act as arguments for other words. Since Forth is stack-based, all arguments are pushed to and popped from the stack. So, when a number token reaches the interpreter, it needs to be pushed onto the data stack.&lt;/p&gt;

&lt;p&gt;To add numbers, we push two numbers onto the data stack and execute the + word. We already know that the math + word is mapped to the #plus symbol in a MathSymbolToken object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;MathSymbolToken&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;mathSymbols&lt;/span&gt;
    &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="nc"&gt;Dictionary&lt;/span&gt; &lt;span class="nf"&gt;newFrom:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;'+'&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;#plus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="s"&gt;'-'&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;#minus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="s"&gt;'*'&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;#star&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="s"&gt;'/'&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;#slash&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this case the ForthToken&amp;gt;&amp;gt;execute inherited behavior is exactly what we need. However, the interpreter must know how to respond to the dynamically sent plus message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthInterpreter&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;plus&lt;/span&gt;
    &lt;span class="nf"&gt;|&lt;/span&gt; &lt;span class="nv"&gt;first&lt;/span&gt; &lt;span class="nf"&gt;second&lt;/span&gt; &lt;span class="nf"&gt;|&lt;/span&gt;
    &lt;span class="nv"&gt;first&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;dataStack&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="nv"&gt;second&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;dataStack&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="nv"&gt;dataStack&lt;/span&gt; &lt;span class="nf"&gt;push:&lt;/span&gt; &lt;span class="nv"&gt;second&lt;/span&gt; &lt;span class="nf"&gt;+&lt;/span&gt; &lt;span class="nv"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, the method is pretty straightforward: it pops the two necessary arguments, performs the calculation, and pushes the result back onto the stack. All other arithmetic messages are resolved in the same way, with the only slightly special case being the slash message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthInterperter&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;slash&lt;/span&gt;
    &lt;span class="nf"&gt;|&lt;/span&gt; &lt;span class="nv"&gt;first&lt;/span&gt; &lt;span class="nf"&gt;second&lt;/span&gt; &lt;span class="nf"&gt;|&lt;/span&gt;
    &lt;span class="nv"&gt;first&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;dataStack&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="nv"&gt;second&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;dataStack&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;first&lt;/span&gt; &lt;span class="nf"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;ifTrue:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nc"&gt;DivisionByZero&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;].&lt;/span&gt;

&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;dataStack&lt;/span&gt; &lt;span class="nf"&gt;push:&lt;/span&gt; &lt;span class="nv"&gt;second&lt;/span&gt; &lt;span class="nf"&gt;/&lt;/span&gt; &lt;span class="nv"&gt;first&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method checks division for zero after pop the two arguments. Why is that? because this behavior is observed in the reference implementations.&lt;/p&gt;

&lt;p&gt;One neat effect of Smalltalk’s features is that our Forth math implementation supports fractions out of the box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Smalltalk-Fraction-Support.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Smalltalk-Fraction-Support.gif" title="Forth Smalltalk Fraction Support" alt="Forth Smalltalk Fraction Support" width="472" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With basic arithmetic working, let’s add some programming tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Tools
&lt;/h2&gt;

&lt;p&gt;In Forth jargon the dot words are the ones that Forth programmers use primarily for debugging and displaying data. The term dot words is derived from the fact that many of these words begins with a dot character. The general purpose of dot words is to facilitate interaction with the stack.&lt;/p&gt;

&lt;p&gt;The first is the dot word:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthInterpreter&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;dot&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;displayAppend:&lt;/span&gt; &lt;span class="nv"&gt;dataStack&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt; &lt;span class="nf"&gt;printString&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This word pops the top element from the stack and prints it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Dot-Word.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Dot-Word.png" title="Forth Dot Word" alt="Forth Dot Word" width="383" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Printing the top element of the data stack with the dot word.&lt;/p&gt;

&lt;p&gt;The dot word is considered part of the core words in the standard. It is commonly used at the end of a one-liner to check results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Dot-Word-One-liner.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Dot-Word-One-liner.gif" title="Forth Dot Word One-liner" alt="Forth Dot Word One-liner" width="400" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another useful tool is the dot S word, which copies and displays the values currently on the data stack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthInterpreter&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;dotS&lt;/span&gt; 
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;displayAppend:&lt;/span&gt;&lt;span class="err"&gt;​&lt;/span&gt; &lt;span class="nv"&gt;dataStack&lt;/span&gt; &lt;span class="nf"&gt;printString&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we simply rely on the printString message of the data stack, as discussed in the stack section earlier.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Dot-S-Word.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Dot-S-Word.png" title="Forth Dot S Word" alt="Forth Dot S Word" width="403" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the tokenization process, the DotWordToken is responsible for mapping the string word to its symbol representation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;DotWordToken&lt;/span&gt; &lt;span class="nf"&gt;class&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;dotWords&lt;/span&gt;
    &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="nc"&gt;Dictionary&lt;/span&gt; &lt;span class="nf"&gt;newFrom:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;'.s'&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;#dotS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="s"&gt;'.'&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;#dot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With these two dot words, we have a basic way to inspect the data stack.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error Handling
&lt;/h2&gt;

&lt;p&gt;We don’t need any sophistication in error handling for now. We just need to create a basic error hierarchy to represent interpretation errors, catch that during the execution of words, and provide an error description to the user when necessary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthIntepreter&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;execute:&lt;/span&gt; &lt;span class="nv"&gt;textWords&lt;/span&gt;
    &lt;span class="err"&gt;...&lt;/span&gt;
    &lt;span class="nv"&gt;tokens&lt;/span&gt; &lt;span class="nf"&gt;do:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;token&lt;/span&gt; &lt;span class="nf"&gt;execute:&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="nf"&gt;on:&lt;/span&gt; &lt;span class="nc"&gt;ForthError&lt;/span&gt;
        &lt;span class="err"&gt;    do:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;error&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;display:&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt; &lt;span class="nf"&gt;description&lt;/span&gt; &lt;span class="p"&gt;]].&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Error-Hierarchy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Error-Hierarchy.png" title="Forth Error Hierarchy" alt="Forth Error Hierarchy" width="277" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The only atypical thing is we specialize the doesNotUndestand: message of the interpreter. The doesNotUndestand: message is sent by the virtual machine to the receiver object when the method lookup fails to find a method for the sent message. It is the virtual machine giving the receiver object the chance to decide how to respond to an unexpected message.&lt;/p&gt;

&lt;p&gt;The doesNotUndestand: message is one of those great ideas built into Smalltalk. It is considered a door to metaprogramming because we can reflect over the aMessage object and respond in very dynamic ways. However, for this case, we wanted something much simpler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthInterpreter&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;doesNotUnderstand:&lt;/span&gt; &lt;span class="nv"&gt;aMessage&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nc"&gt;UndefinedWord&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the interpreter object is the receiver of the Forth words, we want the REPL to display the message ‘Undefined word’ for non-implemented words, rather than triggering the typical Smalltalk behavior of popping up a debugger.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Undefined-Word.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjolisper%2FPublishing%2Frefs%2Fheads%2Fmaster%2F2024%2F11%2F16%2Fassets%2FForth-Undefined-Word.png" title="Forth Undefined Word" alt="Forth Undefined Word" width="391" height="178"&gt;&lt;/a&gt;&lt;br&gt;
For all other errors not subclassed from ForthError, we wanted Smalltalk’s normal behavior.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Editor
&lt;/h2&gt;

&lt;p&gt;To complete the circle of this first iteration of the Forth interpreter, we need to finalize the editor implementation. Recall that when the user presses the return key in the REPL, the processWords message is send to the editor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthEditor&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;processWords&lt;/span&gt;
    &lt;span class="nf"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;extractWords&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;executeWords&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;        &lt;/span&gt;&lt;span class="nv"&gt;printMessageToUser&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;processWords is implemented as a cascade of three private methods, each contributing to the handling of user input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthEditor&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;extractWords&lt;/span&gt;
    &lt;span class="nf"&gt;|&lt;/span&gt; &lt;span class="nv"&gt;textWords&lt;/span&gt; &lt;span class="nf"&gt;|&lt;/span&gt;

    &lt;span class="nv"&gt;textWords&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;model&lt;/span&gt; &lt;span class="nf"&gt;actualContents&lt;/span&gt;
        &lt;span class="nf"&gt;copyFrom:&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;currentLine&lt;/span&gt; &lt;span class="nf"&gt;first&lt;/span&gt;
        &lt;span class="nf"&gt;to:&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;currentLine&lt;/span&gt; &lt;span class="nf"&gt;last&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;extractedWords&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;textWords&lt;/span&gt; &lt;span class="nf"&gt;asString&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The extractWords message retrieves the string of words from the text model and stores it in the extractedWords instance variable, which serves as a pivot for the word-processing logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthEditor&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;executeWords&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;interpreter&lt;/span&gt; &lt;span class="nf"&gt;execute:&lt;/span&gt; &lt;span class="nv"&gt;extractedWords&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This simply sends the execute message to the interpreter, passing the freshly extracted words string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForthEditor&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;printMessageToUser&lt;/span&gt;
&lt;span class="err"&gt;    &lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;addString:&lt;/span&gt; &lt;span class="nv"&gt;interpreter&lt;/span&gt; &lt;span class="nf"&gt;displayMessage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final step is to display feedback to the user in the REPL by printing the appropriate message from the interpreter.&lt;/p&gt;

&lt;p&gt;With the editor logic gluing the REPL and the interpreter: The circle is now complete.&lt;/p&gt;

&lt;h2&gt;
  
  
  Moving Forward
&lt;/h2&gt;

&lt;p&gt;We’ve covered a lot of ground in this article — from crafting a REPL in Smalltalk’s UI to building a Forth interpreter with basic arithmetic, programming tools, and error handling. The effort pays off: it gives us a solid structure to expand the interpreter and experiment with new features in future episodes.&lt;/p&gt;

&lt;p&gt;In the next part, we’ll keep adding features to the interpreter — probably diving into word definitions and conditionals. If you’re curious, make sure to follow along!&lt;/p&gt;

&lt;p&gt;I left you a nerdy-motivational fact about Forth:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Forth programming language was used in the production of Star Wars: Episode IV — A New Hope. Specifically, it was used to control the Dystraflex motion control system, which was created by John Dykstra and his team at Industrial Light &amp;amp; Magic. This system was instrumental in creating the complex and precise camera movements required for the space battle scenes and the trench run sequences on the Death Star.&lt;/p&gt;

&lt;p&gt;From Wikipedia&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Repository: &lt;a href="https://github.com/jolisper/Cuis-Smalltalk-CuiForth" rel="noopener noreferrer"&gt;https://github.com/jolisper/Cuis-Smalltalk-CuiForth&lt;/a&gt;&lt;/p&gt;

</description>
      <category>smalltalk</category>
      <category>forth</category>
      <category>compiler</category>
      <category>interpreter</category>
    </item>
    <item>
      <title>Smalltalk: Conceptual Integrity in Action</title>
      <dc:creator>Jorge Luis Perez</dc:creator>
      <pubDate>Mon, 09 Dec 2024 20:45:38 +0000</pubDate>
      <link>https://dev.to/jolisper/smalltalk-conceptual-integrity-in-action-56j8</link>
      <guid>https://dev.to/jolisper/smalltalk-conceptual-integrity-in-action-56j8</guid>
      <description>&lt;p&gt;One of the most eye-opening and influential ideas in my journey as a programmer was the concept of ‘conceptual integrity’, introduced by Fred Brooks in &lt;em&gt;The Mythical Man-Month&lt;/em&gt; in his essay &lt;em&gt;Aristocracy, Democracy, and System Design&lt;/em&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is better to have a system omit certain anomalous features and improvements, but to &lt;strong&gt;reflect one set of design ideas&lt;/strong&gt;, than to have one that contains many good but independent and uncoordinated ideas.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fred Brooks&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In a nutshell, conceptual integrity consists of a set of well-related, composable concepts and the relationships between them, forming a kind of conceptual algebra.&lt;/p&gt;

&lt;p&gt;When we introduce a new concept that doesn’t integrate well with the others, the conceptual algebra suffers in the form of increased complexity. Special or exceptional cases must be considered, which limits the expressiveness of the algebra and raises the cognitive load for those who have to play with it. Just one poorly selected concept affects the whole.&lt;/p&gt;

&lt;p&gt;In a way, what Brooks is suggesting is that design lives in the constraints we impose on the material we work with. This is especially true in software development. &lt;/p&gt;

&lt;p&gt;Unlike other forms of human craftsmanship, software has more flexible constraints. For example, when building a bridge, we encounter hard limits imposed by factors like gravity, wind, vibrations, and material strength — all of which must be calculated and accounted for before construction even begins.&lt;/p&gt;

&lt;p&gt;Software is, by nature, a very malleable creature, with its hard limits mostly imposed by hardware: the computing power, the volatile and persistent storage, and the capacity of the network connection&lt;sup&gt;1&lt;/sup&gt;&lt;a id="footnote-1-back"&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another point where the analogy between building software and building things in the physical world is also broken is at how those different creations handle the pass of time and the changes, and the expectations we have about that. &lt;/p&gt;

&lt;p&gt;For the things built in the physical world, in general, we expect that those things would be more or less durable and maintain their original characteristics until we finish to used them. No one expects that a bridge automatically gets wider because the traffic that passes over it increases substantially or shrinks when the traffic decreases. That is exactly what we expect from software, we want it to change and adapt automatically. Designing software to show resilience to changes over time is apparently an important feature to consider.&lt;/p&gt;

&lt;p&gt;If we build software like we build bridges, we end up with a piece of software very hard to change and doomed to be maintained until someone with the authority (but probably without the knowledge) decides to destroy it and build it again. Any resemblance with reality is purely coincidental.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Designing by Subtraction&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Subtracting the superfluous to reveal the essence, or the soul, of a creation is a well-known process in arts.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.”&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Saint-Exupéry&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another phrase, attributed to a multi-talented artist primarily known as a sculptor, captures this idea pragmatically.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“I saw the angel in the marble and carved until I set him free.”&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Michelangelo Buonarroti&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I think that we can follow a similar process for software. While the static nature of sculpture might not be a good analogy for a running program, the idea of revealing a design that’s already present in the problem domain sounds like a good guideline to me. The attitude of unveiling a design instead of trying to impose one on the problem should distance us from the root of all evils in programming.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Premature optimization is the root of all evil (or at least most of it) in programming.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Donald Knuth&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To achieve that in software, we should model the problem at hand in a way that no artificial complexity is added to the solution model. Much like a sculptor carving away marble to reveal the form within, we should aim to reduce artificial complexity to a minimum during iterations in software design, refining our solution until the true essence of the problem we’re addressing is revealed.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Problem of Computing Things&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zo83rrdvteahsamfl5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zo83rrdvteahsamfl5t.png" alt="A portrait picture of Alan Turing and other of Alonzo Church side by side" width="672" height="387"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Alan Turing (left) and Alonzo Church (right)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When searching for the roots of computation, we could find two seminal and isomorphic works: Alan Turing’s Turing Machine and Alonso Church’s Lambda Calculus. Discussing these in detail would require an entire article, but it is important to note that each has its own computation metaphor. &lt;/p&gt;

&lt;p&gt;Turing’s approach is more mechanical, with an abstract machine manipulating symbols on an infinite tape, while Church’s Lambda Calculus expresses computation through function abstraction and application, like an ancient ancestor to Lisp.&lt;/p&gt;

&lt;p&gt;Smalltalk also has its own computation metaphor, where the message-passing concept plays a fundamental role. Biological information processing was the source of inspiration for Alan Kay&lt;sup&gt;2&lt;/sup&gt;&lt;a id="footnote-2-back"&gt;&lt;/a&gt;, the way that cells interchange information through protein messages and influences the behavior of the receiving cell is the base model for Object Oriented Programming&lt;sup&gt;3&lt;/sup&gt;&lt;a id="footnote-3-back"&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“The shift in point of view here is […] if you take things like clocks they don’t scale by a factor of a hundred very well. Take things like cells they not only scale by a factor of a hundred but by factors of trillion, and the question is, how do they do it, and how might we adapt this idea for building complex systems.”&lt;sup&gt;4&lt;/sup&gt;&lt;a id="footnote-4-back"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Alan Kay at OOPSLA 1997&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The core idea Kay is illustrating is that biological systems offer a model of scalability and adaptability that can be a useful analogy for thinking about how we design complex software systems.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“The ARPANET, of course, became the Internet, and from the time it started running — just around 1969 or so — to this day, it has expanded by about a factor of a hundred million. […] There is not one line of code in the Internet today that was in the original ARPANET. […] This is a system that has expanded by a hundred million, has changed every atom and every bit, and has never had to stop. &lt;strong&gt;That is the metaphor we absolutely must apply to what we think are smaller things.”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Alan Kay at OOPSLA 1997&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Kay provides the Internet as a practical illustration of how to model systems that mirror the scalability of natural systems. His design of OOP, inspired by biological processes, shares similarities with the modular, decentralized, and dynamic nature of the internet, where objects interact through message-passing much like how nodes in a network communicate.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Communicating Objects&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In Smalltalk, if we want that something happens we must follow the form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;message&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even arithmetic, string concatenation, and control flow follow this pattern. This is where the expression &lt;em&gt;In Smalltalk, everything is an object&lt;/em&gt; finds its deeper meaning. This idea isn’t just a declaration; it’s a practical reality. Everything is an object not because some gods on Olympus declared it, but because the only way to perform any computation is by sending a message to an object — period.&lt;/p&gt;

&lt;p&gt;For instance, the sum of 2 integers is resolved by sending a message &lt;strong&gt;&lt;em&gt;+&lt;/em&gt;&lt;/strong&gt; to the left number, with the right number passed as the message’s argument. Sentences in Smalltalk ends with a dot, similar to a semicolon (&lt;code&gt;;&lt;/code&gt;) in other languages; the result of the evaluation is printed after the dot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="nf"&gt;+&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This marks a fundamental distinction from the vast majority of mainstream programming languages, where such computations are hardcoded into the language’s syntax, even in those that refer to themselves as object-oriented.&lt;/p&gt;

&lt;p&gt;But that decision comes with a cost: Smalltalk loses the familiar operator precedence found in mathematical expressions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="nf"&gt;+&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="nf"&gt;*&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="m"&gt;12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We must be explicit to ensure the common precedence of mathematical operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="nf"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="nf"&gt;*&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Smalltalk sticks to its notion of computation and doesn’t break the object-message computation form just to have a more convenient mathematical precedence. It’s as if Smalltalk were telling us, ‘Sure, having math expressions with well-known operator precedence sounds good, but it doesn’t mesh well with my more important concept of message-passing computation. I’d rather leave that aside.’&lt;/p&gt;

&lt;p&gt;If Smalltalk were to incorporate mathematical expressions with standard operator precedence, it would introduce a special case: math expressions would function as a sublanguage within Smalltalk — a kind of DSL embedded in the main language — increasing the complexity of the implementation, in particular the compiler, which would have to handle arithmetic operators and its precedence. In the current solution, however, the compiler remains completely agnostic to math operators.&lt;/p&gt;

&lt;p&gt;More importantly, including mathematical expressions with standard operator precedence would break the message-passing computation form, affecting the entire conceptual algebra just to incorporate an idea that seems good at first glance. Leaving this idea aside preserves conceptual integrity, resulting in a terser syntax with no special cases.&lt;/p&gt;

&lt;p&gt;In Smalltalk, a number is just like any other object that needs to receive a message to make a computation. This beautifully reminds me of another passage of the same essay from Brooks.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Simplicity and straightforwardness proceed from the conceptual integrity. Every part must reflect the same philosophies and the same balancing of desiderata. Every part must even use the same techniques in syntax and analogous notions in semantics.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fred Brooks&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Even more interesting than arithmetic is the case of control flow. In most programming languages we must use reserved words, such as if-then-else blocks or similar to control the flow of a program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if condition then  
 ...  
else  
 ...  
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is not the case with Smalltalk. Staying true to its computing model, there is no special syntax for controlling program flow — just plain old message sending to objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nv"&gt;condition&lt;/span&gt; &lt;span class="nb"&gt;ifTrue:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="err"&gt;...&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;ifFalse:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="err"&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, the message name is &lt;em&gt;ifTrue:ifFalse:&lt;/em&gt; which receives two blocks: one for the true case and another for the false case. Blocks represent a piece of code—essentially, a set of object collaborations in object-oriented parlance—and they allow us to defer code execution until needed by sending the message &lt;em&gt;value&lt;/em&gt; to the block. In this case, only one of the blocks will be executed, depending on the condition.&lt;/p&gt;

&lt;p&gt;A key point that’s easy to overlook is that the selection of which block to execute depends on the class of the condition object. In Smalltalk, the method &lt;em&gt;ifTrue:ifFalse:&lt;/em&gt; is defined abstractly in the &lt;strong&gt;&lt;em&gt;Boolean&lt;/em&gt;&lt;/strong&gt; class but is implemented differently in its concrete subclasses, &lt;strong&gt;&lt;em&gt;True&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;False&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nc"&gt;True&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;&amp;gt;ifTrue:&lt;/span&gt; &lt;span class="nv"&gt;trueAlternativeBlock&lt;/span&gt; &lt;span class="nf"&gt;ifFalse:&lt;/span&gt; &lt;span class="nv"&gt;falseAlternativeBlock&lt;/span&gt;  
   &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="nv"&gt;trueAlternativeBlock&lt;/span&gt; &lt;span class="nf"&gt;value&lt;/span&gt;

&lt;span class="nf"&gt;False&amp;gt;&amp;gt;ifTrue:&lt;/span&gt; &lt;span class="nv"&gt;trueAlternativeBlock&lt;/span&gt; &lt;span class="nf"&gt;ifFalse:&lt;/span&gt; &lt;span class="nv"&gt;falseAlternativeBlock&lt;/span&gt;  
   &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="nv"&gt;falseAlternativeBlock&lt;/span&gt; &lt;span class="nf"&gt;value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This beautiful implementation illustrates the essence of object-oriented programming: when a message is sent to an object, it is the object itself that determines which method will be used to respond&lt;sup&gt;5&lt;/sup&gt;&lt;a id="footnote-5-back"&gt;&lt;/a&gt;. It’s a fully polymorphic way of understanding control flow.&lt;/p&gt;

&lt;p&gt;Completely aligned with Brooks’ ideas, we can find the following paragraph in the book &lt;em&gt;Smalltalk-80: The Language and Its Implementation&lt;/em&gt;, also known as &lt;em&gt;The Blue Book&lt;/em&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Smalltalk is based on a small number of concepts […] Due to the uniformity with which the object-message orientation is carried out in the system, there are very few new programming concepts to learn in order to understand Smalltalk.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;From Blue Book&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Encapsulation&lt;/strong&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;“Once you have encapsulated in such a way that there is an interface between the inside and the outside it is possible to make an object act like anything, and the reason is simply this, that what you have encapsulated is a computer.”&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Alan Kay at OOPSLA 1997&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Deeply connected to the idea of message-passing computation is the concept of encapsulation. In fact, encapsulation, in some sense, enables message-passing computation to exist: encapsulation creates a boundary between the inside and outside of an object, and it is this boundary that demands an interface to make computation possible. That interface, in turn, allows the object to decide how to respond internally when it receives a message from the outside.&lt;/p&gt;

&lt;p&gt;Thinking of objects as tiny, specialized computers communicating with each other over a network is a powerful analogy. When one computer on the internet communicates with another, it follows a common message protocol to exchange information but doesn’t permit direct access to its volatile or persistent storage. Why? Because allowing an external computer to write directly to its storage would lead to chaos, stripping away its autonomy. This loss of control would break the system’s ability to make decisions, compromising the very foundation of message-passing computation.&lt;/p&gt;

&lt;p&gt;A DTO&lt;sup&gt;6&lt;/sup&gt;&lt;a id="footnote-6-back"&gt;&lt;/a&gt; is a typical example of what an object is not. Being completely open to the outside, it loses its ability to decide what to do, breaking the principle of encapsulation. This unrestricted external access contradicts the autonomy of true objects. A DTO is more like a specialized data structure, similar to a dictionary, that merely mimics an object but lacks its essential characteristics.&lt;/p&gt;

&lt;p&gt;Encapsulation and message-passing computation reinforcing each other in an algebraic form that preserves the system’s conceptual integrity. By conceptualizing objects as autonomous entities, much like specialized computers, we can design modular systems that exhibit uniformity from their most basic components to the most complex constructs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[In] Smalltalk, which is built on the model of communicating objects, […] large applications are viewed in the same way as the fundamental units from which the system is built.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Dan Ingalls in Design Principles Behind Smalltalk&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Late Binding&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Every time we send a message to an object, the Smalltalk virtual machine runs an algorithm called &lt;em&gt;method lookup,&lt;/em&gt; which locates the appropriate method based on the receiver object and message. A key concept here is that the message and the method are mediated by a kind of dynamic switch-case mechanism embedded in the virtual machine &lt;em&gt;for our profit&lt;/em&gt;, effectively embodying the fundamental theorem of computer science that states:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"We can solve any problem by introducing an extra level of indirection."&lt;sup&gt;7&lt;/sup&gt;&lt;a id="footnote-7-back"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This mechanism links the message to the actual implementation code — the method. &lt;strong&gt;The &lt;em&gt;What&lt;/em&gt; and the &lt;em&gt;How&lt;/em&gt; are decoupled&lt;/strong&gt;. This conceptual and practical distinction, between the &lt;em&gt;What&lt;/em&gt; and the &lt;em&gt;How&lt;/em&gt; — between the intention and the realization — is incredibly powerful. Combined with the ubiquity of message-passing computing style, it offers flexibility in virtually every computation we perform.&lt;/p&gt;

&lt;p&gt;In Smalltalk sending a message is a way to express choices.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight smalltalk"&gt;&lt;code&gt;&lt;span class="nv"&gt;myObject&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a message like &lt;em&gt;open&lt;/em&gt; is sent to &lt;em&gt;myObject,&lt;/em&gt; Smalltalk’s method lookup process dynamically determines which implementation of &lt;em&gt;open&lt;/em&gt; to execute, depending on the class of &lt;em&gt;myObject&lt;/em&gt;. Whether &lt;em&gt;myObject&lt;/em&gt; is a file, a window, or a tool, each will interpret and respond to the &lt;em&gt;open&lt;/em&gt; message according to its specific behavior.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Smalltalk provides a […] cleaner solution: it sends the &lt;em&gt;name&lt;/em&gt; of the desired operation, along with any arguments, as a &lt;em&gt;message&lt;/em&gt; to the [object], with the understanding that the receiver knows best how to carry out the desired operation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Dan Ingalls in Design Principles Behind Smalltalk&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At OOPSLA 1997, Alan Kay uses the Japanese concept of &lt;em&gt;ma&lt;/em&gt; to underscore a process-oriented perspective on objects — one that focuses on the space for dynamic interactions and responses within the message-passing model.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Ma is the stuff in-between what we call objects. It’s the stuff we don’t see, because [in the western culture] we’re focused on the nounness of things rather than the processness of things.”&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Alan Kay at OOPSLA 1997&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Ma&lt;/em&gt; refers to an intentional &lt;em&gt;gap&lt;/em&gt; or &lt;em&gt;interval&lt;/em&gt; that allows space for the interpretation of meaning. Sending a message involves more than just a direct action — there’s a conceptual space where the object decides how to respond.&lt;/p&gt;

&lt;p&gt;The separation of &lt;em&gt;What&lt;/em&gt; from &lt;em&gt;How&lt;/em&gt; enables objects to autonomously determine their responses. Smalltalk dynamically aligns intention with execution, holding off the decision of which implementation to execute until the last moment.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“One of the great principles of computer science it’s laziness or whatever you call it, late binding. Hold off decisions when you can.”&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Donald Knuth&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Meta-System&lt;/strong&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;“The more the language can see its own structures the more liberated you can be from the tyranny of a single implementation.”&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Alan Kay at OOPSLA 1997&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the effects of applying the metaphor of communicating objects consistently across Smalltalk is that the language constructs themselves become part of the universe of objects available to us as Smalltalk programmers. And it is exactly this effect that was taken advantage of to create all the visual tools that are part of a typical Smalltalk distribution. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F21r3x5cceog45uqur3m1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F21r3x5cceog45uqur3m1.png" alt="Listing the OrderedCollection class selectors in the accessing category." width="800" height="113"&gt;&lt;/a&gt;&lt;em&gt;Listing the OrderedCollection class selectors in the accessing category.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Debuggers, inspectors, explorers, and browsers in Smalltalk are built with the language itself, utilizing its extensive reflexivity and metaprogramming capabilities. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffwsahip3kw6qmwi9dnlz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffwsahip3kw6qmwi9dnlz.png" alt="Browsing the OrderedCollection class, listing its accessing methods." width="800" height="332"&gt;&lt;/a&gt;&lt;em&gt;Browsing the OrderedCollection class, listing its accessing methods.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Additionally, the visual components of Smalltalk user interface are Smalltalk objects, which means we can inspect, explore, and debug those components just like any other object&lt;sup&gt;8&lt;/sup&gt;&lt;a id="footnote-8-back"&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyenomhngr7q20ahssdr9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyenomhngr7q20ahssdr9.png" alt="Exploring a text editor." width="800" height="317"&gt;&lt;/a&gt;&lt;em&gt;Exploring a text editor&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every component accessible to the user should be able to present itself in a meaningful way for observation and manipulation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Dan Ingalls in Design Principles Behind Smalltalk&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What is even more surprising is that this remarkable level of reflectivity and metaprogramming occurs in an open and live object environment. &lt;em&gt;Open&lt;/em&gt; means that all the objects constituting the base system are accessible for our inspection and modification; &lt;strong&gt;our code operates on the same level as the base system&lt;/strong&gt;. &lt;em&gt;Live&lt;/em&gt; means that we can program, inspect, and modify while the objects are running, freeing us from the conventional edit-compile-run-stop loop.&lt;/p&gt;

&lt;p&gt;Smalltalk people call that the meta-system&lt;sup&gt;9&lt;/sup&gt;&lt;a id="footnote-9-back"&gt;&lt;/a&gt;. The meta-system empowers users to understand and modify the system itself, turning users into creators. Kay envisioned meta-systems as tools to encourage users to not just work with data but to reshape their tools and frameworks as they go. In this view, the user isn’t just operating within a closed system but actively extends, refines, and redefines the system’s boundaries.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“I think one of the things we liked the most about Smalltalk was not what it could do, but the fact that it was such a good vehicle for bootstrapping the next set of ideas we had about how to do systems building.”&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Alan Kay at OOPSLA 1997&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Final Words&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Smalltalk is to me one great example of what we can achieve in system building if we actively follow the concept of conceptual integrity. &lt;/p&gt;

&lt;p&gt;As we’ve seen, Smalltalk is more than just a programming language — it’s an entire interactive programming environment, grounded in a small, interwoven set of concepts applied recursively across both language and system levels. Even more, Smalltalk offers a unique approach to thinking about computation itself. Its metaphor, &lt;em&gt;everything is an object,&lt;/em&gt; is applied uniformly across all areas, reinforcing a powerful and cohesive model of computation.&lt;/p&gt;

&lt;p&gt;Smalltalk, as designed by Alan Kay and his team&lt;sup&gt;10&lt;/sup&gt;&lt;a id="footnote-10-back"&gt;&lt;/a&gt;, aimed to deliver this level of integrity by building a cohesive environment where all components, from the language syntax to the user interface, consistently reflect the object-oriented paradigm.&lt;/p&gt;

&lt;p&gt;By conception, Smalltalk is a tool meant to release the creativity of those who use it, a standpoint for bringing the next big set of ideas about system building. In the words of Alan Kay himself:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[Smalltalk is] an attempt to give the world a bootstrapping mechanism for something much better than Smalltalk.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Alan Kay at OOPSLA 1997&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Licensed under &lt;a href="https://creativecommons.org/licenses/by-sa/4.0/" rel="noopener noreferrer"&gt;CC BY-SA 4.0&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Footnotes:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;[1]: &lt;a id="footnote-1"&gt;&lt;/a&gt; Nowadays with the access to cloud computing those limits are even more relaxed. (back)&lt;/p&gt;

&lt;p&gt;[2]: &lt;a id="footnote-2"&gt;&lt;/a&gt; It’s not a coincidence that those ideas come from Alan Kay; he majored in biology focused on both cell metabolism and larger scale morphogenesis and minored in mathematics centered on abstract algebras. (back)&lt;/p&gt;

&lt;p&gt;[3]: &lt;a id="footnote-3"&gt;&lt;/a&gt; Alan Kay has expressed regret for coining the term &lt;em&gt;Object-Oriented Programming&lt;/em&gt; (OOP). He has criticized himself, saying he should have used a more process-oriented term, as the most important concept is what happens between objects. (back)&lt;/p&gt;

&lt;p&gt;[4]: &lt;a id="footnote-4"&gt;&lt;/a&gt; A quote from Alan Kay: &lt;em&gt;“In many talks, long ago, I have shown a picture with the title “From Gears To Biology” as a metaphor for a qualitative shift in thinking about computer systems — from early-bound, tightly coupled rather small mechanisms — like clocks — to late-bound, loosely coupled rather large mechanisms — like living organisms. In computing, this happened within a single lifetime, and many parts of computing are still stuck in the earlier metaphor.”&lt;/em&gt; (back)&lt;/p&gt;

&lt;p&gt;[5]: &lt;a id="footnote-5"&gt;&lt;/a&gt; In Smalltalk, comparison messages like &lt;em&gt;&amp;gt;&lt;/em&gt;, &lt;em&gt;&amp;lt;&lt;/em&gt; and &lt;em&gt;=&lt;/em&gt; return instances of the &lt;em&gt;True&lt;/em&gt; or &lt;em&gt;False&lt;/em&gt; classes. (back)&lt;/p&gt;

&lt;p&gt;[6]: &lt;a id="footnote-6"&gt;&lt;/a&gt; Data Transfer Object (DTO): Commonly used in many languages, DTOs are objects meant to transport data between systems or layers in an application. They typically have getter and setter methods but lack significant logic, they expose all their fields through public methods. (back)&lt;/p&gt;

&lt;p&gt;[7]: &lt;a id="footnote-7"&gt;&lt;/a&gt; Except for the problem of too many levels of indirections. (back)&lt;/p&gt;

&lt;p&gt;[8]: &lt;a id="footnote-8"&gt;&lt;/a&gt; &lt;em&gt;Morph&lt;/em&gt; is the top-level class for any visual component in Smalltalk (Squeak family). Paraphrasing Smalltalk’s own metaphor: &lt;em&gt;every visual component is a Morph.&lt;/em&gt; (back)&lt;/p&gt;

&lt;p&gt;[9]: &lt;a id="footnote-9"&gt;&lt;/a&gt; A meta-system is a computational system that has as its domain another computational system called its object domain. (back)&lt;/p&gt;

&lt;p&gt;[10]: &lt;a id="footnote-10"&gt;&lt;/a&gt; Mainly Dan Ingalls and Adele Goldberg among others. Dan Ingalls was instrumental in implementing the early versions of Smalltalk, while Adele Goldberg was influential in documenting and promoting its use. (back)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;References:&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/The_Mythical_Man-Month" rel="noopener noreferrer"&gt;The Mythical Man-Month&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=oKg1hTOQXoY&amp;amp;list=PL47Z8KjiFwi4gtoHPQ93MzDkw6rSE-3hC&amp;amp;index=2" rel="noopener noreferrer"&gt;Alan Kay at OOPSLA 1997 — The computer revolution hasn’t happened yet (youtube.com)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=74RdET79q40" rel="noopener noreferrer"&gt;Donald Knuth: Premature optimization is the root of all evil | Donald Knuth and Lex Fridman ()youyoutube.com)&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.quora.com/What-does-Alan-Kay-suggest-here-Computing-is-not-about-clocks-its-about-biology-How-come-the-essence-of-computing-is-more-about-the-way-living-organisms-evolve" rel="noopener noreferrer"&gt;‘Computing is not about clocks, it’s about biology’. How come the essence of computing is more about the way living organisms evolve? — Quora&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=brdx8YAVZag" rel="noopener noreferrer"&gt;Hernan Wilkinson at FAST 2014 — How Smalltalk Affects Thinking (youtube.com)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=blj7itWxk2Y&amp;amp;list=PLMkq_h36PcLCtLKrrdOKKFV2r267VFH_t&amp;amp;index=1" rel="noopener noreferrer"&gt;Aprendiendo Smalltalk con Hernan Wilkinson&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=RhdlBHHimeM" rel="noopener noreferrer"&gt;Glenn Vanderburg — Real Software Engineering (youtube.com)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://stephane.ducasse.free.fr/FreeBooks.html" rel="noopener noreferrer"&gt;Stef’s Free Online Smalltalk Books (Blue Book)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.cs.virginia.edu/~evans/cs655/readings/smalltalk.html" rel="noopener noreferrer"&gt;Design Principles Behind Smalltalk&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.fun-mooc.fr/en/courses/advanced-object-oriented-design-and-development-with-pharo/" rel="noopener noreferrer"&gt;Advanced object-oriented design and development with Pharo — Course — FUN MOOC (fun-mooc.fr)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://scg.unibe.ch/archive/drafts/Duca08a-Sosym-ExecutableMetaLanguage.pdf" rel="noopener noreferrer"&gt;Meta-environment and executable meta-language using Smalltalk: an experience report&lt;/a&gt;&lt;/p&gt;

</description>
      <category>smalltalk</category>
      <category>programming</category>
      <category>oop</category>
      <category>software</category>
    </item>
  </channel>
</rss>
