Rust,Hello World
1. Rust安装
-
步骤
# linux环境安装 curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh # 检查是否安装成功 rustc --version # 安装链接器 apt install build-essential -
遇到的问题
执行安装时遇到
curl: (23) Failure writing output to destination,参考这篇博客解决的解决Ubuntu下安装rust时出现的curl (23) Failure writing output to destination错误
2. vscode配置
参考如何使用VSCode配置Rust开发环境(VS Code 安装 Rust 常用插件) 博客,配置了下面5个插件
- rust-analyzer
- crates
- Rust Syntax
- Even Better TOML
- Rust Test Lens
3. Hello World
-
新建第一个rust项目 hello_world
-
在项目内新建main.rs文件,并将以下代码复制进去
fn main() { println!("Hello, world!"); } -
编译
rustc main.rs -
执行
./main.rs从以上步骤可知
- main是程序执行的入口,没有入参也没有返回值
- 编译和运行是独立的
4. 使用 cargo 管理rust项目
-
使用 cargo 创建项目
cargo new hello_cargo执行完该命令后会生成以下文件结构
-hello_cargo src main.rs .gitignore Cargo.toml -
构建并运行Cargo项目
-
方式一:cargo build & ./target/debug/hello_cargo
这个命令会创建一个可执行文件 target/debug/hello_cargo -
方式二:cargo run
-
-
其他cargo命令
- cargo check:在不生成二进制文件的情况下构建项目来检查错误
- cargo build --release:优化编译项目(可以让 Rust 代码运行的更快,需要消耗更长的编译时间),为用户构建最终程序

浙公网安备 33010602011771号