我有一个类型Instruction并且会使用std::convert::TryFrom从字符串进行转换。
我应该实施 overString或 吗&str?如果我使用&str我有义务使用&*模式或as_ref().
我有类似的东西:Rust Playground 永久链接
use std::convert::TryFrom;
enum Instruction {
Forward, /* other removed for brievity */
}
#[derive(Debug)]
struct InstructionParseError(char);
impl std::convert::TryFrom<&str> for Instruction {
type Error = InstructionParseError;
fn try_from(input: &str) -> Result<Self, Self::Error> {
match input {
"F" => Ok(Instruction::Forward),
_ => unimplemented!(), // For brievity
}
}
}
fn main() {
// I use a string because this input …Run Code Online (Sandbox Code Playgroud) rust ×1