Rust Learning Notes – 01

Introduction – Why I Want to Learn Rust?

I have always wanted to learn a compiled language for several reasons: (1. I saw a requirement in a professor’s lab to master a compiled language; 2. I want to try developing some practical bioinformatics tools, as I currently prefer creating something useful over exploring the biological significance of data analysis.) I have been wavering between C, C++, and Rust, but I never took action to learn, and I blame my procrastination.

Recently, the lab I wanted to join published a very innovative article, rekindling my desire to pursue further studies. (To clarify, I am not just thinking about publishing articles; I believe there will be more possibilities there.) While browsing the lab’s information, I accidentally discovered that a PhD senior I knew from my internship at BGI was actually my roommate for a few days. Although we only shared a room for three or four days, I still know some basic information about him. Later, I learned that he went to that school, but I didn’t expect him to be under that professor.

I searched for more information about this senior online, and he is indeed very outstanding, mastering many skills. Gradually, he became a role model for me during my graduate studies. Seeing his GitHub or tweets recommending Rust, my wavering scale finally tipped towards Rust, and I made up my mind to take action.

If I don’t act now, it will really be too late.

So, let’s get started and learn!!!

PS: I currently have a rough idea for a small tool in mind, and I hope to realize it this year!!

Rust Resources

  1. 1. The Rust Programming Language (Rust Book)

https://course.rs/about-book.html

  1. 2. Beginner’s Tutorial

https://www.runoob.com/rust/rust-tutorial.html

  1. 3. Official Rust Documentation (Chinese)

https://rustwiki.org/

  1. 4. rustlings

https://github.com/rust-lang/rustlings/

  1. 5. Online Rust Playground

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024

  1. 6. Rust By Example

https://doc.rust-lang.org/stable/rust-by-example/index.html

Features of Rust

Let’s briefly understand the features of Rust. I have heard that a major feature is memory safety. I studied C++ in my freshman year, but when I got to pointers, I got distracted and couldn’t really understand that part. Here, I will quote some content from the Beginner’s Tutorial:

  • Memory Safety: Rust’s ownership system prevents dangling pointers, data races, and other memory errors at compile time, eliminating the need for a garbage collector.
  • Concurrent Programming: Rust provides modern language features to support concurrent programming, such as threads and message passing, making it safer and easier to write concurrent programs.
  • Performance: Rust compiles to machine code without a runtime or garbage collector, providing performance close to C and C++.
  • Type System: Rust’s type system and pattern matching provide powerful abstraction capabilities, helping to write safer and more predictable code.
  • Error Handling: Rust’s error handling model encourages explicit handling of all possible error cases.
  • Macro System: Rust provides a powerful macro system that allows developers to write and reuse code at compile time.
  • Package Management: Rust’s package manager Cargo simplifies dependency management and the build process.
  • Cross-Platform: Rust supports multiple operating systems and platforms, including Windows, macOS, Linux, BSDs, etc.
  • Community Support: Rust has an active community that provides a wealth of libraries and tools.
  • Toolchain: Rust has a rich toolchain, including compilers, package managers, documentation generators, etc.
  • No Segmentation Faults: Rust’s ownership and lifetime rules ensure the validity of references, thus avoiding segmentation faults.
  • Iterators and Closures: Rust provides strong support for iterators and closures, simplifying collection processing.

For bioinformatics, Rust can be used to develop tools, which is something I have always wanted to do.

Cargo

In our usual management of bioinformatics environments, we often use conda/pip, while in Rust, we can use Cargo to manage packages.

Creating a Project

Here, we will use cargo to create a world_hello project.

(base) PS D:\000zyf\Learning\rust_learn> cargo new world_hello
    Creating binary (application) `world_hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
(base) PS D:\000zyf\Learning\rust_learn> cd .\world_hello\
(base) PS D:\000zyf\Learning\rust_learn\world_hello> ls
    Directory: D:\000zyf\Learning\rust_learn\world_hello
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           2025/4/12    21:33                src
-a---           2025/4/12    21:33             82 Cargo.toml
(base) PS D:\000zyf\Learning\rust_learn\world_hello>

If you use the command cargo new –vcs git xxx to create a project, it will also generate the corresponding git files.

Complaining: Operating in the Windows terminal is really uncomfortable!!

Running the Project

There are two ways to run:

  1. 1. cargo run
  2. 2. Manually compile and run the project

cargo run

Run cargo run in the newly created project path to see:

(base) PS D:\000zyf\Learning\rust_learn\world_hello> cargo run

   Compiling world_hello v0.1.0 (D:\000zyf\Learning\rust_learn\world_hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 5.83s
     Running `target\debug\world_hello.exe`
Hello, world!

The above code, cargo run first compiles the project and then runs it, so it is actually equivalent to running two commands.

Manual Compilation and Running

(base) PS D:\000zyf\Learning\rust_learn\world_hello> cargo build
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s
(base) PS D:\000zyf\Learning\rust_learn\world_hello> ls
    Directory: D:\000zyf\Learning\rust_learn\world_hello
Mode                 LastWriteTime         Length Name  
----                 -------------         ------ ----  
d----           2025/4/12    21:39                src       
d----           2025/4/12    21:50                target    
-a---           2025/4/12    21:39              8 .gitignore
-a---           2025/4/12    21:50            155 Cargo.lock
-a---           2025/4/12    21:39             82 Cargo.toml
(base) PS D:\000zyf\Learning\rust_learn\world_hello> .\target\debug\world_hello.exe
Hello, world!
(base) PS D:\000zyf\Learning\rust_learn\world_hello> 

It flows smoothly, but it can’t be said to be done in one go. Observant readers may have noticed that in the path when calling, there is a glaring debug field, yes, we are running in debug mode. In this mode, the compilation speed of the code is very fast, but the running speed is slow. The reason is that in debug mode, the Rust compiler does not perform any optimizations, just to complete the compilation as quickly as possible to make your development process smoother.

For example, what if you want high-performance code? Simple, add –release to compile:

cargo run –release

cargo build –release

Try running our high-performance release program:

(base) PS D:\000zyf\Learning\rust_learn\world_hello> cd .\target\         
(base) PS D:\000zyf\Learning\rust_learn\world_hello\target> ls
    Directory: D:\000zyf\Learning\rust_learn\world_hello\target
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           2025/4/12    21:51                debug
-d----           2025/4/12    22:00                release
-a---           2025/4/12    21:51           1120 .rustc_info.json
-a---           2025/4/12    21:50            177 CACHEDIR.TAG
(base) PS D:\000zyf\Learning\rust_learn\world_hello\target> .\release\world_hello.exe
Hello, world!

cargo check

As projects grow larger, cargo run and cargo build inevitably become slower. Is there a faster way to verify the correctness of the code? Here comes the big weapon!

cargo check is the most commonly used command during code development. Its function is simple:to quickly check whether the code can compile. Therefore, this command is very fast and can save a lot of compilation time.

Cargo.toml and Cargo.lock

Cargo.toml and Cargo.lock are the core files of cargo, and all its activities are based on these two.

Cargo.toml is a project data description file unique to cargo. It stores all the metadata configuration information of the project. If Rust developers want their Rust projects to be built, tested, and run as expected, they must construct Cargo.toml in a reasonable way.

The Cargo.lock file is a detailed list of project dependencies generated by the cargo tool based on the same project’s toml file, so we generally do not modify it.

When should you upload Cargo.lock to the git repository? It’s simple: when your project is a runnable program, upload Cargo.lock; if it’s a dependency library project, please add it to .gitignore.

Now, open the “Hello, World” project created above with VSCode, and enter the Cargo.toml file in the root directory. You can see that this file contains a lot of information:

[package]
name = "world_hello"
version = "0.1.0"
edition = "2024"
[dependencies]

The name field defines the project name, the version field defines the current version, which defaults to 0.1.0 for new projects, and the edition field defines the major version of Rust we are using.

Defining Project Dependencies

The greatest advantage of using the cargo tool is that it allows for convenient, unified, and flexible management of various dependencies of the project.

In Cargo.toml, various dependency sections are used to describe the various dependencies of the project:

  1. 1. Based on the Rust official repository crates.io, described by version specifications
  2. 2. Based on the project’s source code git repository address, described by URL
  3. 3. Based on the absolute or relative path of the local project, described by a Unix-like path
  4. Reference
  5. The Rust Programming Language (Rust Course): https://course.rs/first-try/cargo.html

Leave a Comment