通过例子学习Rust

1 你好世界

这是一个传统的 Hello World 程序的源代码:

// This is a comment, and will be ignored by the compiler // You can test this code by clicking the "Run" button over there -> // or if prefer to use your keyboard, you can use the "Ctrl + Enter" shortcut // This code is editable, feel free to hack it! // You can always return to the original code by clicking the "Reset" button -> // This is the main function fn main() { // The statements here will be executed when the compiled binary is called // Print text to the console println!("Hello World!"); }

println! 是一个 宏(Macro) (后面会详细解释),用于输出文本到控制台(Console)。

使用 Rust 编译器 rust 编译该源代码生成可执行文件:

$ rustc hello.rs

上述命令将生成可执行文件 hello

$ ./hello
Hello World!