我正在使用決議器,并且在我的這部分代碼中不斷出現錯誤。
impl FromStr for Binop {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
" " => Binop::Add,
"*" => Binop::Mul,
"-" => Binop::Sub,
"/" => Binop::Div,
"<" => Binop::Lt,
"==" => Binop::Eq,
_ => {return Err(ParseError); } // <---- Error Here
})
}
}
我試過在括號內寫一個實際的字串和一堆其他的東西,我似乎無法理解錯誤的含義。
完整錯誤:
error[E0308]: mismatched types
--> grumpy/src/isa.rs:212:32
|
212 | _ => {return Err(ParseError); }
| ^^^^^^^^^^ expected struct `ParseError`, found fn item
|
::: grumpy/src/lib.rs:17:1
|
17 | pub struct ParseError(String);
| ------------------------------ fn(String) -> ParseError {ParseError} defined here
|
= note: expected struct `ParseError`
found fn item `fn(String) -> ParseError {ParseError}`
help: use parentheses to instantiate this tuple struct
|
212 | _ => {return Err(ParseError(_)); }
|
我將 ParseError 定義為pub struct ParseError(String);
uj5u.com熱心網友回復:
命名元組需要String構造一個值。請注意,字串文字的型別為&str; 您需要呼叫該to_string()方法將其轉換為字串:
Err(ParseError("foo".to_string()))
編譯錯誤指的是fn item因為在這種情況下,它本身ParseError是指隱式建構式,它接受 aString并回傳 a ParseError。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/436497.html
