Rust Programming: A Beginner’s Guide to High-Performance Development

Rust Programming: A Beginner’s Guide to High-Performance Development

Do you want to master a programming language that combines the performance of C++ with modern safety features? Rust is your ideal choice! As a “rising star” in the fields of system programming and high-performance development, Rust is not only adopted by major companies like Firefox and Google Fuchsia, but it is also favored by developers for its beginner-friendly learning path. This article will guide you from installing Rust to writing your first program, allowing you to quickly get started and unlock the infinite possibilities of high-performance development! Follow this beginner’s guide to embark on your Rust programming journey!

This article comprehensively introduces the basics of the Rust programming language, covering its unique advantages in high performance, memory safety, and concurrency, as well as comparisons with languages like C/C++ and Java. Rust is widely used in system programming, WebAssembly, and high-performance web services, and is favored by projects such as Firefox and Google Fuchsia. The article details the installation steps for Rust, configuring the development environment, writing the first “Hello World” program, and managing projects using the Cargo tool. Although the learning curve for Rust is somewhat steep, its powerful performance and safety make it an ideal starting point for beginners aiming for high-performance development.

Introduction to Rust

Why Use Rust?

  • Rust is an exciting new programming language that enables everyone to write reliable and efficient software.
  • It can replace C/C++; Rust offers the same performance, but many common bugs can be eliminated at compile time.
  • Rust is a general-purpose programming language, but it excels in the following scenarios:
    • Need for runtime speed
    • Need for memory safety
    • Better utilization of multi-processors

Comparison with Other Languages

  • C/C++ has excellent performance, but its type system and memory safety are lacking.
  • Java/C# has garbage collection (GC) to ensure memory safety and many excellent features, but performance is not as good.
  • Rust:
    • Safe
    • No GC (better performance and speed)
    • Easy to maintain, debug, and code is safe and efficient

Areas Where Rust Excels

  • High-performance web services (Web API)
  • WebAssembly
  • Command-line tools
  • Network programming
  • Embedded devices
  • System programming

Rust and Firefox

  • Rust was initially a research project at Mozilla. Firefox is an important example of Rust’s application.
  • Mozilla has been using Rust to create an experimental browser engine called Servo, where everything runs in parallel.
    • Some features of Servo have already been integrated into Firefox.
  • The original Quantum version of Firefox included Servo’s CSS rendering engine.
    • Rust has significantly improved Firefox’s performance in this area.

Users and Use Cases of Rust

  • Google: The new operating system Fuchsia, where Rust code accounts for about 30%.
  • Amazon: An operating system developed based on Linux that can run containers directly on bare metal or virtual machines.
  • System76, Baidu, Huawei, Ant Financial…

Advantages of Rust

  • Performance
  • Safety
  • Fearless concurrency

Disadvantages of Rust

  • High learning curve, “difficult to learn”.

Note

  • Rust has many unique concepts that need to be learned step by step.

Installing Rust

Official website: https://www.rust-lang.org/en-US/learn/get-started

Windows: Follow the instructions on the official website.

Mac installation:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Rust Programming: A Beginner's Guide to High-Performance Development

Updating and Uninstalling Rust

  • Update Rust
rustup update
  • Uninstall Rust
rustup self uninstall

Verification of Installation

  • <span>rustc --version</span>
    • Format: rustc x.y.z(abcdbcdbc yyyy-mm-dd)
    • Displays the latest stable version: version number, commit hash, commit date.

Local Documentation

  • When installing Rust, local documentation will be installed for offline browsing.
  • Run <span>rustup doc</span> to open local documentation in the browser.
➜ cargo --version
cargo 1.67.1 (8ecd4f20a 2023-01-10)

~
➜ rustc --version
rustc 1.67.1 (d5a82bbd2 2023-02-07)

~
➜ rustup doc

Development Tools

  • Visual Studio Code
    • Rust plugin
  • Pycharm (IntelliJ IDEA series)
    • Rust plugin

Hello World Example

Writing a Rust program

  • Program file extension: .rs
  • File naming convention: hello_world.rs
➜ mkdir rust

~
➜ cd rust

~/rust
➜ mkdir hello_world

~/rust
➜ cd hello_world

~/rust/hello_world
➜ code .

~/rust/hello_world
➜ pwd
/Users/qiaopengjun/rust/hello_world

~/rust/hello_world via 🦀 1.67.1
➜ mv hello_world.rs main.rs

~/rust/hello_world via 🦀 1.67.1
➜ rustc main.rs

~/rust/hello_world via 🦀 1.67.1
➜ ls
main    main.rs

➜ ./main
Hello World!

The <span>main.rs</span> file

fn main() {
    println!("Hello World!");
}

Anatomy of a Rust Program

  • Defining a function: <span>fn main(){}</span>
    • No parameters, no return value.
  • <span>main</span> function is special: it is the first code that runs in every Rust executable.
  • Printing text: <span>println!("Hello, world!");</span>
    • If it were a function, it wouldn’t have an exclamation mark!
    • Rust uses 4 spaces for indentation instead of tabs.
    • println! is a Rust macro.
    • “Hello World” is a string, which is the argument for println!.
    • This line of code ends with a semicolon.

Compilation and Execution are Two Separate Steps

  • Before running a Rust program, it must be compiled, using the command: <span>rustc</span> source file name.
    • <span>rustc main.rs</span>
  • Upon successful compilation, a binary file will be generated.
    • On Windows, a .pdb file will also be produced, containing debugging information.
  • Rust is an ahead-of-time compiled language.
    • You can compile the program first and then give the executable file to others to run (no need to install Rust).
  • Rustc is suitable only for simple Rust programs…

Hello Cargo

Cargo

  • Cargo is Rust’s build system and package manager.
    • It builds code, downloads dependency libraries, and builds them…
  • Cargo is installed when you install Rust.
    • Cargo –version
~/rust/hello_world via 🦀 1.67.1
➜ cargo --version
cargo 1.67.1 (8ecd4f20a 2023-01-10)

Creating a Project with Cargo

  • Create a project: <span>cargo new hello_cargo</span>
    • Cargo.toml
    • src directory
    • Initializes a new Git repository <span>.gitignore</span>
    • <span>main.rs</span>
    • You can use other VCS or not use VCS: use the –vcs flag when using cargo new.
    • The project name is also <span>hello_cargo</span>
    • A new directory will be created <span>hello_cargo</span>
~/rust
➜ cargo new hello_cargo
     Created binary (application) `hello_cargo` package

~/rust
➜ ls
hello_cargo hello_world

~/rust
➜ cd hello_cargo

hello_cargo on  master [?] via 🦀 1.67.1
➜ ls
Cargo.toml src

hello_cargo on  master [?] via 🦀 1.67.1
➜ ➜ ls
Cargo.toml src

<span>Cargo.toml</span>

  • TOML (Tom’s Obvious, Minimal Language) format is the configuration format for Cargo.
  • [package] is a section header indicating that the content below is for configuring the package.
    • name project name
    • version project version
    • authors project authors
    • edition Rust version used
  • [dependencies] starts another section that lists the project’s dependencies.
  • In Rust, code packages are called crates.
Rust Programming: A Beginner's Guide to High-Performance Development

<span>src/main.rs</span>

  • The main.rs generated by cargo is in the src directory.
  • And Cargo.toml is at the top level of the project.
  • Source code should be in the src directory.
  • The top-level directory can contain: README, license information, configuration files, and other files unrelated to the program source code.
  • If you did not use cargo when creating the project, you can convert the project to use cargo:
    • Move the source code files to src.
    • Create Cargo.toml and fill in the corresponding configuration.

Building a Cargo Project with cargo build

  • cargo build
    • Creates an executable file: target/debug/hello_cargo or target\debug\hello_cargo.exe (Windows).
    • Run the executable file: <span>./target/debug/hello_cargo</span> or <span>. argeteleaseelease_cargo.exe (Windows)</span>
  • The first time you run <span>cargo build</span>, a cargo.lock file will be generated in the top-level directory.
    • This file is responsible for tracking the exact versions of project dependencies.
    • No need to manually modify this file.

Building and Running a Cargo Project <span>cargo run</span>

  • cargo run compiles the code + executes the result.
    • If it has been compiled successfully before and the source code has not changed, it will directly run the binary file.
hello_cargo on  master [?] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo run
   Compiling hello_cargo v0.1.0 (/Users/qiaopengjun/rust/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s
     Running `target/debug/hello_cargo`
Hello, world!

hello_cargo on  master [?] is 📦 0.1.0 via 🦀 1.67.1
➜
Rust Programming: A Beginner's Guide to High-Performance Development

<span>cargo check</span>

  • cargo check checks the code to ensure it can compile, but does not produce any executable files.
  • cargo check is much faster than cargo build.
    • You can repeatedly use cargo check while writing code to improve efficiency.

Building for Release

  • cargo build –release
    • The code will run faster, but the compilation time will be longer.
    • Optimizations will be performed during compilation.
    • The executable file will be generated in target/release instead of target/debug.
  • Two configurations
    • Development
    • Release

Try to use Cargo.

Conclusion

The Rust programming language, with its outstanding performance, memory safety, and modern concurrency capabilities, is becoming the preferred choice in the field of high-performance development. From Firefox’s CSS rendering engine to the Google Fuchsia operating system, the practical applications of Rust demonstrate its powerful potential. This article introduces you to the first steps of learning Rust through installation configuration, the Hello World program, and the Cargo tool. Although Rust’s unique concepts require time to master, its simple syntax and powerful ecosystem allow every developer to easily get started. Install Rust now, write your first high-performance program, and explore the future of programming!

References

  • https://alloy.rs/introduction/getting-started/
  • https://www.rust-lang.org/en-US
  • https://crates.io/
  • https://course.rs/about-book.html
  • https://rustwiki.org/docs/
  • https://rust-book.junmajinlong.com/ch1/00.html

Leave a Comment