about
07/05/2024
RustBites - Introduction
Rust Bites Code

RustBites - Introduction

Why Rust, What are Bites?

Welcome to RustBites! These Bites are a thread of linked webpages that focus on the Rust programming language. You navigate using "Next" and "Prev" buttons on the top and bottom menus, or the pages button on the bottom right. Each Bite provides an introduction to, and discusses the ideas behind, some aspect of the language. The Pages button on the bottom menu, shows things we will be covering in this sequence.

Why Rust?

Rust open-source development has been supported by Mozilla for years. Recently major corporations like Amazon and Facebook have been hiring Rust developers to work full time on development of the Rust compiler and ecosystem. They believe that Rust will be an essential part of their development process. Many Commercial and Industrial Organizations are Using Rust:
The job market for Rust Developers is smaller than for traditional languages like C++, C#, and Java, but is growing rapidly.
Fig 1. Point Class Definition
Fig 2. Using Code
Fig 3. Output

Things you need to know:


  • Rust is designed to support creation of user-defined value types that support construction and cloning of independent instance copies. Managed languages, like C# and Java, copy handles to instances on the heap, so an assignment in those languages results in two handles pointing to the same instance.
  • The Rust language compiles to native code which runs directly in a new process. Code for managed languages run in a virtual machine hosted by a new process. That has consequences for both latency and throughput.
  • Because of its clever type system design and strict static typing, Rust is relatively easy to use. However, for reasons of safety, its references are not allowed to share mutability. The result is that program builds fail repeatedly until all of the safety violations are removed. Compiler error messages are excellent, so fixing them isn't as difficult as it would otherwise be.

Figures 1, 2, and 3, on the right illustrate how Rust programs are structured. Figure 1. holds most of the code for a generic Point class definition. Figure 2. shows how using code can create instances of the class, and Figure 3. displays the output when that code runs. Click on image body to expand. Click on title bar to contract. Here is Point code online in the Rust Playground . Don't be concerned if these figures are hard to understand. We will be covering all of the details in subsequent Bites and in the Rust Story.

Rust Highs

  1. Memory and Data Race Safety
    Compiler enforced data ownership and reference rules insure Memory and Data Race safety.
  2. Performance
    Rust compiles to native code and does not need garbage collection, so it is as fast as C and C++. Here's a comparison.
  3. Error Handling
    Any function that can fail returns a result indicating success or failure. Code has to handle errors in well defined ways.
  4. Simple Value Behavior
    Rust supports value behavior without the need for program developers to define copy and move constructors, assignment operators, and destructors. Developers need only define a single clone operation that is called explicitly. The rest is handled by Rust's unique type system.
  5. Clever design of type system results in fewer context dependencies.
    Rust code describes how its program uses platform resources more accurately than other popular languages.
  6. Rust programs tend to work correctly as soon as they compile.
    There are no memory bugs or race conditions to find and fix.
  7. Very Effective Tool Chain
    The cargo tool creates, manages dependencies, builds, and executes programs and library tests.
  8. Fairly small syntax and library footprint with excellent documentation.
    Makes learning Rust relatively easy.

Rust Lows

  1. Safety restricts the way references can be used. That takes some getting used to. Building Rust code for the first time usually results in a sequence of reference violations that need to be fixed. The compiler has excellent error messages, so that isn't as difficult as it would be otherwise.
  2. Because of the static analysis of reference handling, compile times tend to be longer than for languages like C++.

Getting Started

  • Rust Tool Chain:

    For any environment, download Rust tools. This gives you Cargo, the Rust package manager, rustc, the Rust compiler, and several other tools you will eventually find quite useful. This is all the tooling you need to start.
  • Visual Studio Code

    Rust doesn't come with an IDE, but VS Code gives you a text editor with a terminal pane, from which you enter Cargo commands build, run, clean, and more. You will want to install the VS Code plugin Rust (rls) which gives you some syntax highlighting and code completion. You will find, in the left border, an icon for selecting plugins. Just use the search box to find rls. The VS Code model expects you to use JSON files to configure build and debug launchers. I have found that occasionally hard to use, and often requires more effort to configure than I am willing to spend on this tool. Working in the code editor and launching Cargo commands in the terminal works well for me. For Windows, I use the powershell (PS) terminal, but that can be replaced with the cmd prompt. On linux the default bash terminal works well.
  • Starting Code

    Create an empty RustCode folder, open a terminal, and type:
    > cargo new hello
    That creates a Rust package that contains a hello world program. Enough to show you how to create a function and print something to the console. This works because the Rust installation put cargo on your path.
    When the package has been created - a second or two - type the command:
    > cargo run
    You will see cargo invoke rustc, the Rust compiler, then run the resulting executable.
    > cargo --help
    and
    > cargo --list
    will show you other things that cargo can do for you.
    Note:
    In order to build and run with cargo from the Visual Studio Code terminal you need to open VS Code in the package folder for the code you want to build and run. That's the folder where the package cargo.toml file resides.
    If you open VS Code and, using the file menu, open the folder you created above, you will see a cargo.toml file, which holds metadata for the package, and a src folder. Open that and select main.rs. There you will see a main function which simply prints "hello world" in your terminal.
    That's all there is to do to get started. The world is yours! -- at least the Rust programming world :-)

References:

Reference Description
Jon Gjengset Considering Rust - why should you explore Rust?
RustTour.pdf Quick tour of the Rust programming language emphasizing its unique attributes.
  Next Prev Pages Sections About Keys