要入门Rust编程,首先需要安装Rust编程环境并创建一个Hello World项目。以下是步骤:
首先,你需要安装Rust编程环境。你可以使用rustup,它是Rust的官方工具,用于安装和管理Rust的不同版本。打开终端并运行以下命令:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
根据提示,选择你需要的选项进行安装。
安装完成后,可以验证Rust是否正确安装。在终端中运行以下命令:
rustc --version
这将显示Rust编译器的版本信息。
现在,你可以创建一个新的Rust项目。打开终端并运行以下命令:
cargo new hello_world
这将创建一个名为 hello_world 的新项目目录,并自动生成一个Cargo.toml文件和一个src/main.rs文件。
打开项目目录,并编辑src/main.rs文件,将以下代码添加到文件中:
- fn main() {
- println!("Hello, World!");
- }
这是一个非常简单的Rust程序,它打印"Hello, World!"到终端。
在项目目录中,你可以使用Cargo构建工具来编译和运行你的Hello World程序。在终端中执行以下命令:
- cd hello_world
- cargo run
Cargo将编译你的程序并运行它,你应该在终端中看到"Hello, World!"的输出。
这就是创建和运行一个简单的Hello World项目的全部步骤。现在,你已经入门了Rust编程,可以继续学习更多关于Rust的知识和编程技巧。
- use std::fs;
- use std::io;
- use std::env;
-
- fn visit_dirs(dir: &std::path::Path) -> io::Result<()> {
- if dir.is_dir() {
- for entry in fs::read_dir(dir)? {
- let entry = entry?;
- let path = entry.path();
- if path.is_file() {
- println!("File: {:?}", path.display());
- } else if path.is_dir() {
- println!("Directory: {:?}", path.display());
- visit_dirs(&path)?;
- }
- }
- }
- Ok(())
- }
-
- fn main() -> io::Result<()> {
- let args: Vec<String> = env::args().collect();
- println!("{:?}", args);
- println!("size = {}", args.len());
- let tmppath = &args[1];
- let path = std::path::Path::new(tmppath); // 替换为你要遍历的文件夹路径
- visit_dirs(path)?;
- Ok(())
- }
cargo run G:\mypath