一、安装和配置

rust安装(linux)

1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

cargo创建项目

1
2
3
cargo new hello_world
cd helloworld
cargo run

cargo创建的项目分为:

  • bin项目:可直接运行的二进制项目
  • lib项目:库类型项目,不可以直接运行,而是作为第三方库被引用

当我们使用cargo new project时,默认创建的项目为bin项目。

helloworld

约定俗成地,需要写一个helloworld:

1
2
3
fn main() {
println!("Hello, world!");
}

入口

以cargo创建的bin项目入口为src/main.rs内的main函数。

1
2
3
fn main() {
println!("Hello, world!");
}

在项目根目录使用cargo run即可运行项目。

二、rust基础篇