要在Linux上编译和运行Rust程序,请按照以下步骤操作:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按照提示操作,完成安装过程。
cargo
命令创建一个新的Rust项目。例如,要创建一个名为hello_world
的项目,请运行以下命令:cargo new hello_world
这将在当前目录下创建一个名为hello_world
的新文件夹,其中包含一个简单的Rust程序。
hello_world/src/main.rs
文件,并编写你的Rust代码。例如,你可以编写一个简单的“Hello, world!”程序:fn main() {
println!("Hello, world!");
}
Cargo.toml
文件的目录)下运行以下命令以编译项目:cargo build
这将编译你的Rust程序并生成一个可执行文件。默认情况下,可执行文件将位于hello_world/target/debug
目录下。
./hello_world/target/debug/hello_world
你应该看到输出“Hello, world!”。
注意:如果你想为发布版本编译程序,可以使用cargo build --release
命令。发布版本的可执行文件将位于hello_world/target/release
目录下,性能更高,但编译时间更长。要运行发布版本,请使用./hello_world/target/release/hello_world
命令。