Rust学习: hello world

121 阅读2分钟

入坑Rust,语言通常喜欢以输出 hello world开始,那今天先玩玩hello world

安装

参考官方文档:doc.rust-lang.org/book/ch01-0…

开始输出

首先是简单的hello world

fn main() {
    println!("Hello, world!");
}

这里遇到了错误### error: linker link.exe not found,安装Visual Studio 2022 可解决

一般到这里第一课就结束了,但是感觉没吃饱(确定不是吃太饱?),继而初步了解了一下这个Rust数据类型以及流程控制,于是结合官网的猜数字程序,当猜对时输出hello world

初步实现如下:

use std::io;
use rand::Rng;
fn main() {
    // println!("Hello, world!");
    let secret_number = rand::thread_rng().gen_range(1..=100);

    loop {
        let mut guess = String::new();
        io::stdin().read_line(&mut guess)
            .expect("read error");
        let guess_num = guess.parse::<i32>().unwrap();
        if secret_number == guess_num {
            println!("Hello, world!");
            break;
        }else if guess_num > secret_number {
            println!("太大了");
        }else{
            println!("太小了");
        }
    }
}

接着准备执行一下后结束第一天的学习,不出意外的话,应该是出意外了。

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }', src\main.rs:12:46
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library\std\src\panicking.rs:593
   1: core::panicking::panic_fmt
             at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library\core\src\panicking.rs:67
   2: core::result::unwrap_failed
             at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library\core\src\result.rs:1651
   3: enum2$<core::result::Result<i32,core::num::error::ParseIntError> >::unwrap<i32,core::num::error::ParseIntError>
             at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26\library\core\src\result.rs:1076
   4: rustNew::main
             at .\src\main.rs:12
   5: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26\library\core\src\ops\function.rs:250
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

查找资料后,发现加了trim之后就不报错了,查询了一下read_line方法会读取用户输入的整行内容,包括换行符\n,证实一下这个答案,在用户输入数字代码后加上两行输出

let mut guess = String::new();
io::stdin().read_line(&mut guess)
    .expect("read error");
print!("用户输入了:{guess}");
print!("我很明显是不用换行的");

发现果然,在使用不换行的输出函数进行输出,竟然也换行了

123
用户输入了:123
我很明显是不用换行的

重新调整代码,并增加异常处理:

use std::io;
use rand::Rng;
fn main() {
    // println!("Hello, world!");
    let secret_number = rand::thread_rng().gen_range(1..=100);

    loop {
        let mut guess = String::new();
        io::stdin().read_line(&mut guess)
            .expect("read error");
        let guess_num:i32 = match guess.trim().parse(){
            Ok(num) => num,
            Err(_) => {println!("输入的不是数字,再来过");continue},
        };
        if secret_number == guess_num {
            println!("Hello, world!");
            break;
        }else if guess_num > secret_number {
            println!("太大了");
        }else{
            println!("太小了");
        }
    }
}
  • 运行结果:
adasda
输入的不是数字,再来过
12312adasd
输入的不是数字,再来过
12312
太大了
12
太小了
34
太小了
54
太小了
67
太大了
55
太小了
56
太小了
57
Hello, world!