预期类型 `Method` 找到枚举 `Result<_, MethodError>`
Expected type `Method` found enum `Result<_, MethodError>`
我正在尝试将 &str
转换为 enum
,但在 Err(e) => Err(e)
这行出现错误 Expected type Method found enum Result<_, MethodError>
。在这种情况下如何正确 return 出错?
use std::str::FromStr;
fn main() {
#[derive(Debug)]
pub enum Method {
GET,
DELETE,
POST,
PUT,
HEAD,
CONNECT,
OPTIONS,
TRACE,
PATCH,
}
impl FromStr for Method {
type Err = MethodError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"GET" => Ok(Self::GET),
"DELETE" => Ok(Self::DELETE),
"POST" => Ok(Self::POST),
"PUT" => Ok(Self::PUT),
"HEAD" => Ok(Self::HEAD),
"CONNECT" => Ok(Self::CONNECT),
"OPTIONS" => Ok(Self::OPTIONS),
"TRACE" => Ok(Self::TRACE),
"PATCH" => Ok(Self::PATCH),
_ => Err(MethodError),
}
}
}
pub struct MethodError;
let method:&str = "GET";
let methodE = match method.parse::<Method>() {
Ok(method) => method,
Err(e) => Err(e),
};
}
感谢大家的意见,我想通了:
main
需要 return Result<(), MethodError>
return
匹配臂中需要关键字 Err(e) => return Err(e)
use std::str::FromStr;
#[derive(Debug)]
pub enum Method {
GET,
DELETE,
POST,
PUT,
HEAD,
CONNECT,
OPTIONS,
TRACE,
PATCH,
}
impl FromStr for Method {
type Err = MethodError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"GET" => Ok(Self::GET),
"DELETE" => Ok(Self::DELETE),
"POST" => Ok(Self::POST),
"PUT" => Ok(Self::PUT),
"HEAD" => Ok(Self::HEAD),
"CONNECT" => Ok(Self::CONNECT),
"OPTIONS" => Ok(Self::OPTIONS),
"TRACE" => Ok(Self::TRACE),
"PATCH" => Ok(Self::PATCH),
_ => Err(MethodError),
}
}
}
#[derive(Debug)]
pub struct MethodError;
fn main() -> Result<(), MethodError> {
let method:&str = "GET";
let methodE = match method.parse::<Method>() {
Ok(method) => method,
Err(e) => return Err(e),
};
// this works too: let methodE: Method = method.parse()?;
Ok(())
}
我正在尝试将 &str
转换为 enum
,但在 Err(e) => Err(e)
这行出现错误 Expected type Method found enum Result<_, MethodError>
。在这种情况下如何正确 return 出错?
use std::str::FromStr;
fn main() {
#[derive(Debug)]
pub enum Method {
GET,
DELETE,
POST,
PUT,
HEAD,
CONNECT,
OPTIONS,
TRACE,
PATCH,
}
impl FromStr for Method {
type Err = MethodError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"GET" => Ok(Self::GET),
"DELETE" => Ok(Self::DELETE),
"POST" => Ok(Self::POST),
"PUT" => Ok(Self::PUT),
"HEAD" => Ok(Self::HEAD),
"CONNECT" => Ok(Self::CONNECT),
"OPTIONS" => Ok(Self::OPTIONS),
"TRACE" => Ok(Self::TRACE),
"PATCH" => Ok(Self::PATCH),
_ => Err(MethodError),
}
}
}
pub struct MethodError;
let method:&str = "GET";
let methodE = match method.parse::<Method>() {
Ok(method) => method,
Err(e) => Err(e),
};
}
感谢大家的意见,我想通了:
main
需要 returnResult<(), MethodError>
return
匹配臂中需要关键字Err(e) => return Err(e)
use std::str::FromStr;
#[derive(Debug)]
pub enum Method {
GET,
DELETE,
POST,
PUT,
HEAD,
CONNECT,
OPTIONS,
TRACE,
PATCH,
}
impl FromStr for Method {
type Err = MethodError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"GET" => Ok(Self::GET),
"DELETE" => Ok(Self::DELETE),
"POST" => Ok(Self::POST),
"PUT" => Ok(Self::PUT),
"HEAD" => Ok(Self::HEAD),
"CONNECT" => Ok(Self::CONNECT),
"OPTIONS" => Ok(Self::OPTIONS),
"TRACE" => Ok(Self::TRACE),
"PATCH" => Ok(Self::PATCH),
_ => Err(MethodError),
}
}
}
#[derive(Debug)]
pub struct MethodError;
fn main() -> Result<(), MethodError> {
let method:&str = "GET";
let methodE = match method.parse::<Method>() {
Ok(method) => method,
Err(e) => return Err(e),
};
// this works too: let methodE: Method = method.parse()?;
Ok(())
}