gpt4 book ai didi

RuSTLings Errors3.rs 从字符串转换错误 - 未为 `std::string::String` 实现特征

转载 作者:行者123 更新时间:2023-12-03 11:26:01 32 4
gpt4 key购买 nike

我正在学习 RuSTLings 类(class) Errors3.rs :

// This is a program that is trying to use a completed version of the
// `total_cost` function from the previous exercise. It's not working though!
// Why not? What should we do to fix it?

use std::num::ParseIntError;

fn main() {
let mut tokens = 100;
let pretend_user_input = "8";

let cost = total_cost(pretend_user_input)?;

if cost > tokens {
println!("You can't afford that many!");
} else {
tokens -= cost;
println!("You now have {} tokens.", tokens);
}
}

pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>()?;

Ok(qty * cost_per_item + processing_fee)
}

这是我当前的代码:

use std::num::ParseIntError;

fn main() -> Result<String, String> {
let mut tokens = 100;
let pretend_user_input = "8";

let cost = total_cost(pretend_user_input)?;

if cost > tokens {
//println!("You can't afford that many!");
Ok(format!("You can't afford that many!"))
} else {
tokens -= cost;
//println!("You now have {} tokens.", tokens);
Err(format!("You now have {} tokens.", tokens).to_string())
}

}

pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>()?;

Ok(qty * cost_per_item + processing_fee)
}

我不知道如何纠正第一个错误:

error[E0277]: `?` couldn't convert the error to `std::string::String`
--> src/main.rs:7:46
|
7 | let cost = total_cost(pretend_user_input)?;
| ^ the trait `std::convert::From<std::num::ParseIntError>` is not implemented for `std::string::String`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= help: the following implementations were found:
<std::string::String as std::convert::From<&std::string::String>>
<std::string::String as std::convert::From<&str>>
<std::string::String as std::convert::From<std::borrow::Cow<'a, str>>>
<std::string::String as std::convert::From<std::boxed::Box<str>>>
= note: required by `std::convert::From::from`

error[E0277]: `main` has invalid return type `std::result::Result<std::string::String, std::string::String>`
--> src/main.rs:3:14
|
3 | fn main() -> Result<String, String> {
| ^^^^^^^^^^^^^^^^^^^^^^ `main` can only return types that implement `std::process::Termination`
|
= help: consider using `()`, or a `Result`

根据建议,关于 From,我尝试将 Ok(format!("You can't afford that many!")) 更改为 Ok (String::from("你买不起那么多!"))。但它会导致几乎相同的错误消息。

我已经尝试查看 std::convert::From 的 Rust 文档.这给了我尝试的想法:


let slug: &'static str = "You can't afford that many!";
if cost > tokens {
//println!("You can't afford that many!");
Ok(std::convert::From(slug))
} else {
tokens -= cost;
//println!("You now have {} tokens.", tokens);
Err(format!("You now have {} tokens.", tokens).to_string())
}

导致错误的原因:

error[E0423]: expected function, tuple struct or tuple variant, found trait `std::convert::From`
--> src/main.rs:12:12
|
12 | Ok(std::convert::From(slug))
| ^^^^^^^^^^^^^^^^^^ not a function, tuple struct or tuple variant

最佳答案

你试图改变原始程序的行为,程序必须打印一些东西而不是从主程序返回一个字符串(实际上你不能从主程序返回一个字符串,你必须返回一些实现了 Termination 的东西)。

解决方案与您所做的接近,main() 也必须返回错误,有两种方法,使用真实类型或使用动态特征。你的情况很简单,所以真正的类型是最简单的:

use std::num::ParseIntError;

fn main() -> Result<(), ParseIntError> {
let mut tokens = 100;
let pretend_user_input = "8";

let cost = total_cost(pretend_user_input)?;

if cost > tokens {
println!("You can't afford that many!");
} else {
tokens -= cost;
println!("You now have {} tokens.", tokens);
}

// we just return ok with nothing in it, this mean program terminated without error
Ok(())
}

pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>()?;

Ok(qty * cost_per_item + processing_fee)
}

但你也可以使用动态特征,它更先进但不是特别好:

use std::num::ParseIntError;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
let mut tokens = 100;
let pretend_user_input = "8";

let cost = total_cost(pretend_user_input)?;

if cost > tokens {
println!("You can't afford that many!");
} else {
tokens -= cost;
println!("You now have {} tokens.", tokens);
}

Ok(())
}

pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>()?;

Ok(qty * cost_per_item + processing_fee)
}

在 Rust 中有很多方法可以处理错误。你可以在this blog post上学到很多东西.

关于RuSTLings Errors3.rs 从字符串转换错误 - 未为 `std::string::String` 实现特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59492825/

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