ฉันต้องการใช้คุณสมบัติที่กำหนดเองสำหรับ &'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>
เป็นไปได้ไหมที่จะใช้ UiId
สำหรับ &'a str
และทุกสิ่งที่สามารถแปลงเป็น i32
โดยไม่ต้องระบุประเภทที่เป็นรูปธรรม ฉันจะทำเช่นนั้นได้อย่างไร?