我有一个类型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) 我想min()对方法f32,u32和i32,所以我创建了一个特点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 ×2