相关疑难解决方法(0)

实现 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
查看次数

为什么我没有实现没有实现Ord的f32的"特征冲突实现"?

我想min()对方法f32,u32i32,所以我创建了一个特点Min:

trait Min {
    fn min(v1: Self, v2: Self) -> Self;
}

impl<T> Min for T where T: Ord {
    fn min(v1: Self, v2: Self) -> Self {
        ::std::cmp::min(v1, v2)
    }
}

impl Min for f32 {
    fn min(v1: Self, v2: Self) -> Self {
        v1.min(v2)
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

error[E0119]: conflicting implementations of trait `Min` for type `f32`:
  --> src/main.rs:11:1
   |
5  | / impl<T> Min for T where T: …
Run Code Online (Sandbox Code Playgroud)

rust

5
推荐指数
1
解决办法
484
查看次数

标签 统计

rust ×2