AI Summary 116.17392.20 Rust 输出到命令行摘要 2026-09-23
helloworld use Cargo! Cargo is Rust’s build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code,

Rust 命令行

println!( "Hello World") 中的 println 后面还有一个 ! 符号,难道 Rust 函数之后都要加一个感叹号?
println 不是函数,而是一个宏规则。
这里不需要更深刻的挖掘宏规则是什么,后面的章节中会专门介绍,并不影响接下来的一段学习。

Rust 输出文字的方式主要有两种:
println!() 和 print!()
这两个"函数"都是向命令行输出字符串的方法,区别仅在于前者会在输出的最后附加输出一个换行符。
当用这两个"函数"输出信息的时候,第一个参数是格式字符串,后面是一串可变参数,对应着格式字符串中的"占位符",这一点与 C 语言中的 printf 函数很相似。
但是,Rust 中格式字符串中的占位符不是 "% + 字母" 的形式,而是一对 {}

实例:soho.rs 文件

fn main() {
let a = 12;
println!("a is {}", a);
}
使用 rustc 命令编译 soho.rs 文件:

$ rustc soho.rs   # 编译 soho.rs 文件

编译后会生成 soho 可执行文件

$ ./soho    # 执行 soho

输出结果

a is 12 

把 a 输出两遍,那岂不是要写成:

println!("a is {}, a again is {}", a, a); 

更好的写法

println!("a is {0}, a again is {0}", a); 

在 {} 之间可以放一个数字,将把之后的可变参数当作一个数组来访问,下标从 0 开始

如果要输出 { 或 } 怎么办呢?格式字符串中通过 {{ 和 }} 分别转义代表 { 和 }
但是其他常用转义字符与 C 语言里的转义字符一样,都是反斜杠开头的形式

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

输出结果

{}


downloading the libraries your code depends on, and building those libraries. (We call the libraries that your code needs dependencies.)
$ cargo --version
cargo 1.98.1 (797e8a9bc 2026-08-05)
If you see a version number, you have it!
If you see an error, such as command not found, look at the documentation for your method of installation to determine how to install Cargo separately.

Creating a Project with Cargo
Let’s create a new project using Cargo and look at how it differs from our original “Hello, world!” project.
Navigate back to your projects directory (or wherever you decided to store your code). Then, on any operating system, run the following:
$ cd /data/rust/
$ cargo new helloworld
$ cd helloworld

The first command creates a new directory and project called helloworld.
We’ve named our project helloworld, and Cargo creates its files in a directory of the same name.
Go into the helloworld directory and list the files.
You’ll see that Cargo has generated two files and one directory for us:
a Cargo.toml file
a src directory with a main.rs file inside

It has also initialized a new Git repository along with
a .gitignore file
Git files won’t be generated if you run cargo new within an existing Git repository;
you can override this behavior by using cargo new --vcs=git.


Open Cargo.toml in your text editor of choice.
Filename: Cargo.toml
This file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format.

$ cat Cargo.toml

[package]
name = “helloworld”
version = “0.1.0”
edition = “2024”

[dependencies]

The first line, [package], is a section heading that indicates that the following statements are configuring a package.
As we add more information to this file, we’ll add other sections.

The next three lines set the configuration information Cargo needs to compile your program:
the name, the version, and the edition of Rust to use.

The last line, [dependencies], is the start of a section for you to list any of your project’s dependencies.
In Rust, packages of code are referred to as crates.

Now open src/main.rs and take a look:
Filename: src/main.rs
$ cat src/main.rs

fn main() {
  println!(“Hello, world!”);
}


Building and Running a Cargo Project
Now let’s look at what’s different when we build and run the “Hello, world!” program with Cargo!
From your helloworld directory,
build your project by entering the following command:

$ cargo build
Finished dev profile [unoptimized + debuginfo] target(s) in 2.41s


This command creates an executable file in target/debug/helloworld (or target\debug\helloworld.exe on Windows) rather than in your current directory.
Because the default build is a debug build, Cargo puts the binary in a directory named debug.
You can run the executable with this command:
$ ./target/debug/helloworld
# or
.\target\debug\helloworld.exe on Windows

Hello, world!

If all goes well,
Hello, world! should print to the terminal.
Running cargo build for the first time also causes Cargo to create a new file at the top level: Cargo.lock.
This file keeps track of the exact versions of dependencies in your project.
This project doesn’t have dependencies, so the file is a bit sparse.
You won’t ever need to change this file manually;
Cargo manages its contents for you.

Filename: Cargo.lock
$ cat Cargo.lock

# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = “helloworld”
version = “0.1.0”


We just built a project with cargo build and ran it with ./target/debug/helloworld,
also use cargo run to compile the code and then run the resultant executable all in one command:

$ cargo run
Finished dev profile [unoptimized + debuginfo] target(s) in 0.03s
Running target/debug/helloworld
Hello, world!

Using cargo run is more convenient than having to remember to run cargo build and then use the whole path to the binary, so most developers use cargo run.

Notice that this time we didn’t see output indicating that Cargo was compiling helloworld.
Cargo figured out that the files hadn’t changed, so it didn’t rebuild but just ran the binary.
If you had modified your source code, Cargo would have rebuilt the project before running it, and you would have seen this output:

$ cargo run
Compiling helloworld v0.1.0 (file:///projects/helloworld)
Finished dev [unoptimized + debuginfo] target(s) in 0.33 secs
Running target/debug/helloworld
Hello, world!


Cargo also provides a command called cargo check.
This command quickly checks your code to make sure it compiles but doesn’t produce an executable:

$ cargo check
Checking helloworld v0.1.0 (/data/rust/helloworld)
Finished dev profile [unoptimized + debuginfo] target(s) in 0.09s

Why would you not want an executable?
Often, cargo check is much faster than cargo build because it skips the step of producing an executable.
If you’re continually checking your work while writing the code, using cargo check will speed up the process of letting you know if your project is still compiling!
As such, many Rustaceans run cargo check periodically as they write their program to make sure it compiles.
Then, they run cargo build when they’re ready to use the executable.

Let’s recap what we’ve learned so far about Cargo:
create a project using cargo new.
build a project using cargo build.
build and run a project in one step using cargo run.
build a project without producing a binary to check for errors using cargo check.


Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.
An additional advantage of using Cargo is that the commands are the same no matter which operating system you’re working on.
So, at this point, we’ll no longer provide specific instructions for Linux and macOS versus Windows.

Building for Release
When project is finally ready for release, use
$ cargo build --release
to compile it with optimizations.
This command will create an executable in
target/release
instead of
target/debug.
The optimizations make Rust code run faster, but turning them on lengthens the time it takes for program to compile.
This is why there are two different profiles: one for development, when you want to rebuild quickly and often, and another for building the final program you’ll give to a user that won’t be rebuilt repeatedly and that will run as fast as possible.
If you’re benchmarking your code’s running time, be sure to run cargo build --release and benchmark with the executable in target/release.

Leveraging Cargo’s Conventions
With simple projects, Cargo doesn’t provide a lot of value over just using rustc,
but it will prove its worth as your programs become more intricate.
Once programs grow to multiple files or need a dependency, it’s much easier to let Cargo coordinate the build.

Even though the helloworld project is simple, it now uses much of the real tooling you’ll use in the rest of your Rust career.
In fact, to work on any existing projects, you can use the following commands to check out the code using Git, change to that project’s directory, and build:

$ git clone example.org/someproject
$ cd someproject
$ cargo build
For more information about Cargo, check out its documentation.

Summary
You’re already off to a great start on your Rust journey!