about
Bits Hello Rust
09/02/2023
0
Bits Repo Code Bits Repo Docs

Bits:  Rust Hello

code, output, and build for Rust on Windows, macOS, and Linux

These pages support comparison of fragments of code in several different languages, much as you might compare a sentence of English with one in Spanish to help you learn Spanish or English.

Synopsis:

This page demonstrates use of Visual Studio Code to build and execute a simple Rust "Hello World" program, showing its build process and launch file. The purpose is to show how to get started quickly.
  • Companion pages for Rust, C#, Python, and JavaScript are accessible from links in the left panel.
  • Navigation through example levels for the same language is effected with the "Bits Pages" button in the left panel, or "Next" and "Prev" buttons in the menus or by using the "N" and "P" keys.
  • The page links in the center of the left panel point to major sections of this page.
Note:
Most page links in this site refer to specific points in a target page, usually the top. For all of the code bits here, language links return to the last scroll position. This supports quickly moving between similar code for each of the languages covered, and maintains context for comparisons.

Source Code

Code below is a fragment taken from Bits Repository. You can download the repo containing code for all Bits by opening the "Code" dropdown in that page.
// HelloWorld::main.rs

fn main() {
    println!("Hello Rust world");
}          

Output

This is output from the Rust source code above, running in Visual Studio Code. The output is retrieved in an embedded terminal window. VS Code runs on Windows, Linux, and macOS.
C:\github\JimFawcett\Bits\Rust\rust_hello_world
> cargo run -q

Hello Rust world

C:\github\JimFawcett\Bits\Rust\rust_hello_world
>

Build

To build a Rust project with Cargo you use two commands, cargo build to build the project, and cargo run to build and run the project. Cargo expects to be run from the project directory, the one holding Cargo.toml file and a "src" directory holding all the project files. With the help of the Rust RLS plug-in, you can build from the VS Code menu, but I find it simpler, and quicker to build from the command line in VS Code's embedded terminal.
C:\github\JimFawcett\Bits\Rust\rust_hello_world
> cargo build
   Compiling rust_hello_world v0.1.0 (C:\github\JimFawcett\Bits\Rust\rust_hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.31s
C:\github\JimFawcett\Bits\Rust\rust_hello_world

Create Project

The terminal session below shows how to create a project for Rust using the Cargo tool (part of the Rust installation). See tooling information here: Bits_Tooling.
C:\github\JimFawcett\test
> cargo new --bin rust_hello_world
     Created binary (application) `rust_hello_world` package
C:\github\JimFawcett\test
> cd rust_hello_world
C:\github\JimFawcett\test\rust_hello_world
> dir

    Directory: C:\github\JimFawcett\test\rust_hello_world

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/23/2023   3:11 PM                src
-a----         2/23/2023   3:11 PM            185 Cargo.toml

C:\github\JimFawcett\test\rust_hello_world
> type Cargo.toml
[package]
name = "rust_hello_world"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
C:\github\JimFawcett\test\rust_hello_world
>

2.0 VS Code View

The code for this demo is available in github.com/JimFawcett/Bits. If you click on the Code dropdown you can clone the repository of all code for these demos to your local drive. Then, it is easy to bring up any example, in any of the languages, in VS Code. Here, we do that for Cpp\Cpp_Hello. Figure 1. VS Code IDE Figure 2. Launch.JSON Figure 3. Rust Plugins

3.0 References

ReferenceDescription
Rust Story E-book with seven chapters covering most of intermediate Rust
Rust Bites Relatively short feature discussions
Bits Tooling Tool chains, VS Code
You will find many additional references in the Rust Story and Rust Bites.