# rust-template **Repository Path**: T-T33code/rust-template ## Basic Information - **Project Name**: rust-template - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-11-19 - **Last Updated**: 2026-02-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # rust template ### 开发工具 Vscode ### 插件安装 1. Dependi 依赖管理 2. Even Better TOML toml文件支持 3. Better Comments 优化注解显示 4. Error Lens 优化错误提示 5. GitLens GIT增强 6. indent-rainbow 锁进优化提示 7. Prettier - Code formatter 代码格式化 8. Rest Client REST api 9. rust-analyzer rust语法支持 10. Rust Test Lens rust的test支持 11. Rust Test Explorer rust测试概览 12. TODO Highlight 待完成高量 13. vscode-icons 图标 14. YAML 文件支持 ### cargo-generate 工具安装 cargo install cargo-generate ### 使用模板(模板可以是任何代码地址) cargo generate demo/template cargo generate demo/template --name rcli git clone https://github.com/demo/template.git rcli ### 代码提交检查工具 pip install pre-commit pre-commit install --hook-type pre-push ### cargo deny cargo install --locked cargo-deny ### cargo install typos-cli cargo install git-cliff cargo install cargo-nextest --locked ### 1. 猜字谜游戏 安装依赖(随机数生成):cargo add rand ```rust use rand::Rng; fn main() { println!("Guess the number!"); let secret_number = rand::rng().random_range(1..=100); loop { println!("Please input your guess."); let mut guess = String::new(); std::io::stdin() .read_line(&mut guess) .expect("Failed to read line"); println!("You guessed: {guess}"); // 将字符串转换为数字,如果不能转化就继续 let guess: u32 = match guess.trim().parse() { Ok(num) => num, Err(_) => continue, }; match guess.cmp(&secret_number) { std::cmp::Ordering::Less => println!("Too small!"), std::cmp::Ordering::Greater => println!("Too big!"), std::cmp::Ordering::Equal => { println!("You win!"); break; } } } } ``` ### 2. 变量与不可变性 x的值不能被修改,编译报错 ```rust fn main() { let x = 5; println!("The value of x is: {x}"); x = 6; println!("The value of x is: {x}"); } ``` mut关键字,声明,x的值可以被修改 ```rust fn main() { let mut x = 5; println!("The value of x is: {x}"); x = 6; println!("The value of x is: {x}"); } ``` ### 3.遮蔽特性 ```rust fn main() { let x = 5; // x的值是5 let x = x + 1; //声明一个新x为6(5+1) { let x = x * 2; //在此作用域中声明一个x为12(6*2) println!("The value of x in the inner scope is: {x}"); //当前的x为12 } println!("The value of x is: {x}"); // 当前的x为6 } ``` ```shell $ cargo run Compiling variables v0.1.0 (file:///projects/variables) Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.31s Running `target/debug/variables` The value of x in the inner scope is: 12 The value of x is: 6 ``` 如果字符串42 转为数字,需要加数字类型,比如u32(也是遮蔽性) ```rust let guess = "42"; let guess: u32 = guess.parse().expect("Not a number!"); ``` ### 4.函数 最基本的简单的函数 ```rust fn main() { println!("Hello, world!"); another_function(); } fn another_function() { println!("Another function."); } ``` 带参数的函数 ```rust fn main() { print_labeled_measurement(5, 'h'); } fn print_labeled_measurement(value: i32, unit_label: char) { println!("The measurement is: {value}{unit_label}"); } ``` 将括号作用域中的最终数字赋值给y,所有y的结果为4 注意:x+1没有分号 ```rust fn main() { let y = { let x = 3; x + 1 }; println!("The value of y is: {y}"); } ``` 带返回值的函数 ```rust fn five() -> i32 { 5+5 } fn main() { let x = five(); println!("The value of x is: {x}"); } ``` ### 5.控制语句 ```rust fn main() { let number = 3; if number < 5 { println!("condition was true"); } else { println!("condition was false"); } } ``` if为一个表达式,可以在let语句的右侧进行使用 ```rust fn main() { let condition = true; let number = if condition { 5 } else { 6 }; println!("The value of number is: {number}"); } ``` loop 、while、for等循环控制 ### 6. 结构体 ```rust struct User { active: bool, username: String, email: String, sign_in_count: u64, } ``` 如果所有的类型是一样的,则可以创建一个元组结构体 ```rust struct Color(i32, i32, i32); struct Point(i32, i32, i32); fn main() { let black = Color(0, 0, 0); let origin = Point(0, 0, 0); } ``` 没有任何字段的类单元结构体 ```rust struct AlwaysEqual; ``` ```rust struct Rectangle { width: u32, height: u32, } fn main() { let rect1 = Rectangle { width: 30, height: 50, }; println!( "The area of the rectangle is {} square pixels.", area(&rect1) ); } fn area(rectangle: &Rectangle) -> u32 { rectangle.width * rectangle.height } ``` 为结构体显示的设置打印的功能 ```rust #[derive(Debug)] struct Rectangle { width: u32, height: u32, } fn main() { let rect1 = Rectangle { width: 30, height: 50, }; println!("rect1 is {rect1:?}"); } ``` 使用dbg!宏:打印出代码中调用 dbg! 宏时所在的文件和行号,以及该表达式的结果值,并返回该值的所有权。 ```rust #[derive(Debug)] struct Rectangle { width: u32, height: u32, } fn main() { let scale = 2; let rect1 = Rectangle { width: dbg!(30 * scale), height: 50, }; dbg!(&rect1); } ``` 为结构体创建一个结构体内部的方法 ```rust #[derive(Debug)] struct Rectangle { width: u32, height: u32, } impl Rectangle { fn area(&self) -> u32 { self.width * self.height } } fn main() { let rect1 = Rectangle { width: 30, height: 50, }; println!( "The area of the rectangle is {} square pixels.", rect1.area() ); } ``` ### 枚举值 ```rust enum IpAddrKind { V4, V6, } ``` ```rust enum IpAddrKind { V4, V6, } struct IpAddr { kind: IpAddrKind, address: String, } let home = IpAddr { kind: IpAddrKind::V4, address: String::from("127.0.0.1"), }; let loopback = IpAddr { kind: IpAddrKind::V6, address: String::from("::1"), }; ``` ```rust enum IpAddr { V4(u8, u8, u8, u8), V6(String), } let home = IpAddr::V4(127, 0, 0, 1); let loopback = IpAddr::V6(String::from("::1")); ``` ### match控制流结构 ```rust enum Coin { Penny, Nickel, Dime, Quarter, } fn value_in_cents(coin: Coin) -> u8 { match coin { Coin::Penny => 1, Coin::Nickel => 5, Coin::Dime => 10, Coin::Quarter => 25, } } //也可以执行多行代码,但是需要使用大括号包裹起来 fn value_in_cents(coin: Coin) -> u8 { match coin { Coin::Penny => { println!("Lucky penny!"); 1 } Coin::Nickel => 5, Coin::Dime => 10, Coin::Quarter => 25, } } ``` ```rust let dice_roll = 9; match dice_roll { 3 => add_fancy_hat(), 7 => remove_fancy_hat(), _ => reroll(), } fn add_fancy_hat() {} fn remove_fancy_hat() {} fn reroll() {} ```