Rust 如何使用impl<';a>;从<&';一种方法>;for&';锈迹斑斑?
enumRust 如何使用impl<';a>;从<&';一种方法>;for&';锈迹斑斑?,rust,Rust,enum方法有一个trait实现,这是不言自明的: impl<'a> From<&'a Method> for &'a str { fn from(v: &'a Method) -> Self { match v { Method::Describe => "DESCRIBE", Method::GetParameter => "
方法
有一个trait实现,这是不言自明的:
impl<'a> From<&'a Method> for &'a str {
fn from(v: &'a Method) -> Self {
match v {
Method::Describe => "DESCRIBE",
Method::GetParameter => "GET_PARAMETER",
Method::Options => "OPTIONS",
Method::Pause => "PAUSE",
Method::Play => "PLAY",
Method::PlayNotify => "PLAY_NOTIFY",
Method::Redirect => "REDIRECT",
Method::Setup => "SETUP",
Method::SetParameter => "SET_PARAMETER",
Method::Teardown => "TEARDOWN",
Method::Extension(ref v) => v,
}
}
}
无法工作,因为into()
仅适用于str
我发现这是可行的:
let s: &str = request.method().into();
let x: String = s.into();
但是,我能在一行中完成所有工作吗?我想你可以这样做:
让x:String=From:::From(a.into());
让y:String=Into:::Into(a).to_owned();
或者,您可以从实现中为字符串定义新的:
impl<'a> From<&'a Method> for String {
fn from(x: &'a Method) -> Self {
let x: &str = x.into();
x.to_owned()
}
}
impl for String{
fn来自(x:&'a方法)->Self{
设x:&str=x.into();
x、 对()
}
}
然后您可以这样调用它:let z:String=a.into()
impl<'a> From<&'a Method> for String {
fn from(x: &'a Method) -> Self {
let x: &str = x.into();
x.to_owned()
}
}