软件研发 --- hello world 项目 之 底层之秀 rust
https://gitee.com/null_465_7266/rust4hellowrld
Rust Hello World 项目
这是一个最小化的 Rust Hello World 项目,用于演示 Rust 的基本使用方法。
环境安装
1. 安装 Rust
Windows 系统
方法一:使用 rustup-init.exe(推荐)
-
下载安装程序
- 访问 Rust 官网
- 点击 "Download rustup-init.exe" 下载安装程序
- 或者直接访问:https://win.rustup.rs/x86_64
-
运行安装程序
- 双击下载的
rustup-init.exe文件 - 如果出现安全警告,点击"更多信息" → "仍要运行"
- 双击下载的
-
安装过程
- 安装程序会打开命令行窗口
- 看到提示时,按
Enter键选择默认安装选项 - 安装程序会自动下载并安装 Rust 工具链
- 安装完成后,按任意键关闭窗口
-
配置环境变量
- 安装程序会自动配置环境变量
- 重启命令提示符或 PowerShell
- 或者重新打开 VS Code 终端
方法二:使用 PowerShell 命令
如果您更喜欢命令行安装,可以在 PowerShell 中运行:
# 下载并运行 rustup 安装脚本
Invoke-WebRequest -Uri "https://win.rustup.rs/x86_64" -OutFile "rustup-init.exe"
.\rustup-init.exe
方法三:使用包管理器
如果您已安装 Chocolatey 或 Scoop:
# 使用 Chocolatey
choco install rust
# 使用 Scoop
scoop install rustup
安装注意事项
- 确保您有管理员权限
- 安装过程需要网络连接
- 安装大小约 400MB
- 如果您使用的是企业网络,可能需要配置代理设置
macOS 系统
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
Linux 系统
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
2. 验证安装
安装完成后,需要验证 Rust 是否正确安装:
步骤 1:打开新的终端
- 关闭所有现有的命令提示符或 PowerShell 窗口
- 重新打开命令提示符(cmd)或 PowerShell
- 或者在 VS Code 中打开新的终端(Ctrl + Shift + `)
步骤 2:检查 Rust 版本
rustc --version
cargo --version
预期输出示例:
rustc 1.75.0 (82e1608df 2023-12-21)
cargo 1.75.0 (1d8b05cdd 2023-11-20)
步骤 3:检查安装路径(Windows)
# 检查 Rust 安装路径
where rustc
where cargo
# 检查环境变量
echo $env:PATH | Select-String "cargo"
如果命令无法识别:
- 重启计算机:有时需要重启才能使环境变量生效
- 手动添加环境变量:
- 打开"系统属性" → "高级" → "环境变量"
- 在"用户变量"中找到
PATH - 确保包含:
%USERPROFILE%\.cargo\bin
- 重新安装:如果问题持续,可能需要重新运行安装程序
验证成功标志:
- 命令能正常执行且显示版本信息
- 没有"命令未找到"或"不是内部或外部命令"的错误
项目结构
rust-hello-world/
├── Cargo.toml # 项目配置文件
├── src/
│ └── main.rs # 主程序文件
└── README.md # 项目说明文档
代码说明
Cargo.toml
这是 Rust 项目的配置文件,包含项目的元数据和依赖信息:
name: 项目名称version: 项目版本edition: Rust 版本(2021 是最新的稳定版本)dependencies: 项目依赖(当前为空)
src/main.rs
这是主程序文件,包含一个简单的 main 函数:
fn main() {
println!("Hello, World!");
}
编译和运行
方法一:使用 cargo run(推荐)
cargo run
这个命令会自动编译并运行程序。
方法二:分步编译和运行
# 编译项目
cargo build
# 运行编译后的可执行文件(Windows)
./target/debug/rust-hello-world.exe
# 运行编译后的可执行文件(macOS/Linux)
./target/debug/rust-hello-world
发布版本编译
# 编译优化版本
cargo build --release
# 运行优化版本(Windows)
./target/release/rust-hello-world.exe
# 运行优化版本(macOS/Linux)
./target/release/rust-hello-world
常用 Cargo 命令
cargo new <项目名>- 创建新项目cargo build- 编译项目cargo run- 编译并运行项目cargo check- 检查代码是否能编译(不生成可执行文件)cargo test- 运行测试cargo clean- 清理编译产物cargo update- 更新依赖
预期输出
运行程序后,您应该看到以下输出:
Hello, World!
下一步
现在您已经成功创建并运行了第一个 Rust 程序!您可以:
- 修改
src/main.rs中的代码来尝试不同的功能 - 学习 Rust 的基本语法和概念
- 添加依赖库到
Cargo.toml来扩展功能 - 探索 Rust 的官方文档:https://doc.rust-lang.org/
故障排除
Windows 常见问题
-
"cargo 无法将项识别为 cmdlet" 错误
cargo : 无法将"cargo"项识别为 cmdlet、函数、脚本文件或可运行程序的名称解决方案:
- 重启计算机(推荐)
- 重新打开 PowerShell 或命令提示符
- 检查环境变量是否正确设置
- 重新运行 rustup-init.exe
-
环境变量问题 手动添加环境变量:
- 按
Win + R,输入sysdm.cpl - 点击"高级" → "环境变量"
- 在"用户变量"中编辑
PATH - 添加:
%USERPROFILE%\.cargo\bin - 点击"确定"并重启终端
- 按
-
防火墙或杀毒软件阻止
- 临时关闭防火墙和杀毒软件
- 将 Rust 安装目录添加到白名单
- 重新运行安装程序
-
网络连接问题
# 如果在企业网络环境,可能需要设置代理 $env:HTTPS_PROXY = "http://proxy.company.com:8080" $env:HTTP_PROXY = "http://proxy.company.com:8080" -
磁盘空间不足
- 确保 C 盘有至少 1GB 可用空间
- Rust 工具链会安装到
%USERPROFILE%\.rustup和%USERPROFILE%\.cargo
通用问题
-
编译错误
- 检查代码语法是否正确
- 确保
Cargo.toml格式正确 - 运行
cargo check检查语法
-
项目路径问题
- 避免在路径中使用中文字符
- 确保项目路径不包含空格
- 使用英文路径名
-
版本兼容性问题
# 更新 Rust 到最新版本 rustup update # 检查当前版本 rustup show
获取帮助
如果遇到其他问题:
- 官方文档:https://doc.rust-lang.org/
- Rust 论坛:https://users.rust-lang.org/
- GitHub Issues:https://github.com/rust-lang/rust/issues
- 中文社区:https://rustcc.cn/
完全重新安装
如果问题无法解决,可以完全重新安装:
# 卸载 Rust
rustup self uninstall
# 删除残留文件(可选)
Remove-Item -Recurse -Force "$env:USERPROFILE\.rustup"
Remove-Item -Recurse -Force "$env:USERPROFILE\.cargo"
# 重新下载并安装
Invoke-WebRequest -Uri "https://win.rustup.rs/x86_64" -OutFile "rustup-init.exe"
.\rustup-init.exe
浙公网安备 33010602011771号