Rust 基本类型的机具特性(浮动)

Rust 基本类型的机具特性(浮动),rust,numbers,polymorphism,traits,parametric-polymorphism,Rust,Numbers,Polymorphism,Traits,Parametric Polymorphism,我想处理环,所以我有一个特性RingOps,我想float成为它的一部分。我认为float实现了每个超类型,因此派生将非常好,但如果不是,如何实现呢 trait RingOps: Add<Output=Self> + Mul<Output=Self> + Eq + Debug where Self: std::marker::Sized {} impl RingOps for float {} Rust中没有float类型,您必须分别为f32和f64实现此

我想处理环,所以我有一个特性
RingOps
,我想
float
成为它的一部分。我认为
float
实现了每个超类型,因此派生将非常好,但如果不是,如何实现呢

trait RingOps: Add<Output=Self> + Mul<Output=Self> + Eq + Debug
    where Self: std::marker::Sized {}
  
impl RingOps for float {}

Rust中没有
float
类型,您必须分别为
f32
f64
实现此功能。例如:

使用std::fmt::Display;
特点:显示{
fn打印(&self){
println!(“我是{}”,self);
}
}
f32{}的impl特征
f64{}的impl特征
fn main(){
1.5_f32.print();//打印“我是1.5”
1.5_f64.print();//打印“我是1.5”
}

酷!这让我比我想象的更进一步,是不是
f32
没有实现
std::cmp::Eq
?啊,nvm发现了它,这是一个设计决策,值得注意的是,有一个提供有用的
Eq
Ord
,和
哈希
实例(一个新类型的浮点包装器)。这些实现不符合IEEE标准(因为IEEE没有满足所有这些特性的要求),但它们非常方便,而且在我看来,比IEEE定义更直观。
    error[E0412]: cannot find type `float` in this scope
 --> src/main.rs:8:18
  |
8 | impl RingOps for float {}
  |                  ^^^^^ not found in this scope

error[E0277]: the trait bound `{float}: RingOps` is not satisfied
  --> src/main.rs:44:32
   |
13 |     Input(&'a str, T),
   |     ----------------- required by `Circuit::Input`
...
44 |         Box::new(Circuit::Input("a", 2.0)),
   |                                      ^^^ the trait `RingOps` is not implemented for `{float}`