RustBites: Hello

hello world program

Synopsis:

This page demonstrates using Visual Studio Code to build and run a simple Rust "Hello World" program, showing its build process and launch file. The goal is to get you started quickly.
  1. Source Code The code below is a fragment from the Bits Repository. Download the repo containing all Bits code by opening the "Code" dropdown on that page.
    // HelloWorld::main.rs
    fn main() {
      println!("Hello Rust world");
    }          
  2. Output This is the output from the Rust source code above, running in Visual Studio Code. An embedded terminal window displays the output. 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 Building a Rust project with Cargo uses two commands: cargo build builds the project, and cargo run builds and runs it. Run Cargo from the project directory - the one holding Cargo.toml and a "src" directory that contains all the project files. The Rust RLS plug-in lets you build from the VS Code menu, but I find building from the command line in VS Code's embedded terminal simpler and quicker.
    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 Rust project 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 lives at github.com/JimFawcett/Bits. Click the Code dropdown to clone the repository of all demo code to your local drive. From there, opening any example in any language in VS Code is easy. 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.