gpt4 book ai didi

Rust 书,猜谜游戏类型不匹配

转载 作者:行者123 更新时间:2023-11-29 07:45:05 28 4
gpt4 key购买 nike

我正在学习 1.1.0 的 rust book 教程,但尝试运行他们的代码时出现错误。

我有以下内容:

extern crate rand;

use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main() {
println!("Guess the number!");

let secret_number = rand::thread_rng().gen_range(1, 101);

println!("The secret number is: {}", secret_number);

println!("Please input your guess.");

let mut guess = String::new();

io::stdin().read_line(&mut guess)
.ok()
.expect("failed to read line");

println!("You guessed: {}", guess);

match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => println!("You win!"),
}
}

运行后我得到

src/main.rs:24:21: 24:35 error: mismatched types:
expected `&collections::string::String`,
found `&_`
(expected struct `collections::string::String`,
found integral variable) [E0308]
src/main.rs:24 match guess.cmp(&secret_number) {
^~~~~~~~~~~~~~
src/main.rs:24:21: 24:35 help: run `rustc --explain E0308` to see a detailed explanation

这段代码是直接从教程中复制粘贴过来的,有什么问题吗?

最佳答案

没有错。该教程实际上解释了为什么这不会编译:

I did mention that this won’t quite work yet, though. Let’s try it: ... Whew! This is a big error. The core of it is that we have ‘mismatched types’. Rust has a strong, static type system.

关于Rust 书,猜谜游戏类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31078185/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com