Rust Bites Code

RustBites: Hello

hello world program

The next few 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.
  1. 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");
    }          
  2. 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
    >
  3. 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
          
  4. 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
    >
          
  5. 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
  6. 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.