rust学习笔记~
最近终于闲下来一点了决定有空要好好学习rust! fn main() {
println!("Hello World!");
} fn main() {
println!("Hello, world!");
let mut guess = String::new();
std::io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
println!("You guessed: {}", guess);
}
use rand::Rng;
fn main() {
println!("Hello, world!");
let mut guess = String::new();
let secret_number = rand::thread_rng().gen_range(1..=100);
loop {
println!("Please input your guess.");
guess.clear(); // 清空上一次的输入
std::io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
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;
},
}
}
}
nb,哥哥怎么都转rust了 王一之 发表于 2026-4-5 22:27
nb,哥哥怎么都转rust了
一直都想学一下rust,但是之前也学的半懂不懂的
现在感觉确实rust使用的地方越来越多了。。。
该提上日程了 王一之 发表于 2026-4-5 22:27
nb,哥哥怎么都转rust了
按我现在的理解我觉得ai是挺利好rust的。。。 struct Rect {
x: i32,
y: i32,
}
fn main() {
let rect1 = Rect { x: 30, y: 50 };
println!("The area of the rectangle is {} square pixels.", area(&rect1));
}
fn area(rectangle: &Rect) -> i32 {
rectangle.x * rectangle.y
} struct Rect {
x: i32,
y: i32,
}
impl Rect {
fn area(&self) -> i32 {
self.x * self.y
}
fn new(x: i32, y: i32) -> Rect {
Rect { x, y }
}
}
fn main() {
let rect1 = Rect { x: 30, y: 50 };
println!("The area of the rectangle is {} square pixels.", rect1.area());
let rect2 = Rect::new(30, 50);
println!("The area of the rectangle is {} square pixels.", rect2.area());
}
页:
[1]