李恒道 发表于 2026-4-5 21:17:46

rust学习笔记~

最近终于闲下来一点了
决定有空要好好学习rust!

李恒道 发表于 2026-4-5 21:24:11

fn main() {
    println!("Hello World!");
}

李恒道 发表于 2026-4-5 21:30:44

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);
}

李恒道 发表于 2026-4-5 21:44:24

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;
            },
      }
    }

}

王一之 发表于 2026-4-5 22:27:01

nb,哥哥怎么都转rust了

李恒道 发表于 2026-4-6 11:37:09

王一之 发表于 2026-4-5 22:27
nb,哥哥怎么都转rust了

一直都想学一下rust,但是之前也学的半懂不懂的
现在感觉确实rust使用的地方越来越多了。。。
该提上日程了

李恒道 发表于 2026-4-6 11:50:08

王一之 发表于 2026-4-5 22:27
nb,哥哥怎么都转rust了

按我现在的理解我觉得ai是挺利好rust的。。。

李恒道 发表于 2026-4-6 12:30:35

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
}

李恒道 发表于 2026-4-6 12:30:39

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]
查看完整版本: rust学习笔记~