小编Dar*_*ria的帖子

实现 TryFrom 我应该使用 &str 还是 String

我有一个类型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

6
推荐指数
1
解决办法
2735
查看次数

标签 统计

rust ×1