"expected struct `ParseError`, found fn item" 是什么意思?
What does "expected struct `ParseError`, found fn item" mean?
我正在处理解析器,但我的这部分代码一直出错。
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);
命名元组需要构造一个 String
值。请注意,字符串文字的类型为 &str
;您需要调用 to_string()
方法将其转换为字符串:
Err(ParseError("foo".to_string()))
编译错误指的是 fn item
,因为在这种情况下,ParseError
本身指的是隐式构造函数,它采用 String
和 returns ParseError
.
我正在处理解析器,但我的这部分代码一直出错。
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);
命名元组需要构造一个 String
值。请注意,字符串文字的类型为 &str
;您需要调用 to_string()
方法将其转换为字符串:
Err(ParseError("foo".to_string()))
编译错误指的是 fn item
,因为在这种情况下,ParseError
本身指的是隐式构造函数,它采用 String
和 returns ParseError
.