Hello, World
让我们进入最简单的 Rust 程序,一个经典的 Hello World 程序:
fn main() { println!("Hello 🌍!"); }
你看到的:
- 函数以
fn
开头。 - 像 C 和 C++ 一样,块由花括号分隔。
main
函数是程序的入口点。- Rust 有卫生宏 (hygienic macros),
println!
就是一个例子。 - Rust 字符串是 UTF-8 编码的,可以包含任何 Unicode 字符。
This slide should take about 5 minutes.
This slide tries to make the students comfortable with Rust code. They will see a ton of it over the next four days so we start small with something familiar.
关键点:
-
Rust is very much like other languages in the C/C++/Java tradition. It is imperative and it doesn't try to reinvent things unless absolutely necessary.
-
Rust 是一门现代编程语言,它完全支持 Unicode 等特性。
-
Rust uses macros for situations where you want to have a variable number of arguments (no function overloading).
-
宏是“卫生的”,这意味着它们不会意外地捕获它们所在作用域中的标识符。实际上,Rust 的宏只是部分卫生。