【发布时间】:2016-08-26 05:57:47
【问题描述】:
我想为&'a str 和最大为i32 的整数实现一个自定义特征,但Rust 不允许我这样做:
use std::convert::Into;
pub trait UiId {
fn push(&self);
}
impl<'a> UiId for &'a str {
fn push(&self) {}
}
impl<T: Into<i32>> UiId for T {
fn push(&self) {}
}
fn main() {}
编译失败,出现以下错误:
error[E0119]: conflicting implementations of trait `UiId` for type `&str`:
--> src/main.rs:11:1
|
7 | impl<'a> UiId for &'a str {
| ------------------------- first implementation here
...
11 | impl<T: Into<i32>> UiId for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&str`
|
= note: upstream crates may add new impl of trait `std::convert::From<&str>` for type `i32` in future versions
&'a str 没有实现Into<i32>。是否可以为&'a str 实现UiId 以及可以转换为i32 的所有内容而不指定具体类型?我该怎么做?
【问题讨论】:
-
我认为这一定是 Rust 确定实现是否重叠(或可能重叠)的限制,但我还没有找到任何地方说明规则的地方。 :-( 哦,对于正确的语言规范!