简体   繁体   English

将 const 关联到 Rust 中的特征不能按照教程中的描述工作

[英]Associated const to a trait in Rust not working as described in the tutorial

I am trying to execute the same code as described here我正在尝试执行与此处描述的相同的代码

(Page ~448) (第~448页)

Playground 操场

But when I try to execute this sample I get:但是当我尝试执行这个示例时,我得到:

error[E0038]: the trait `Float` cannot be made into an object


--> src\main.rs:35:25
   |
35 |     let fibonacci_of_3: dyn Float = fibonacci(3);
   |                         ^^^^^^^^^ `Float` cannot be made into an object
   |
   = help: consider moving `ZERO` to another trait
   = help: consider moving `ONE` to another trait
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
  --> src\maths.rs:4:11
   |
3  | pub trait Float {
   |           ----- this trait cannot be made into an object...
4  |     const ZERO: Self;
   |           ^^^^ ...because it contains this associated `const`
5  |     const ONE: Self;
   |           ^^^ ...because it contains this associated `const`

Here is the Fibonacci function:这是斐波那契 function:

pub fn fibonacci<T: Float + Add<Output=T>>(n: usize) -> T {
    match n {
        0 => T::ZERO,
        1 => T::ONE,
        n => fibonacci::<T>(n - 1) + fibonacci::<T>(n - 2),
    }
}

About the dyn, without it I get:关于 dyn,没有它我得到:

error[E0782]: trait objects must include the `dyn` keyword
  --> src\main.rs:34:25
   |
34 |     let fibonacci_of_3: Float = fibonacci(3);
   |                         ^^^^^
   |
help: add `dyn` keyword before this trait
   |
34 |     let fibonacci_of_3: dyn Float = fibonacci(3);
   |                         +++

Is the information outdated or am I doing something wrong?, in paper makes sense.信息是过时的还是我做错了什么?,在纸上是有道理的。

Regards问候

~M ~M

You need to specify the types (not the trait itself) you want to use:您需要指定要使用的类型(而不是特征本身):

use std::ops::Add;

trait Float {
    const ZERO: Self;
    const ONE: Self;
}

impl Float for f32 {
    const ZERO: f32 = 0.0;
    const ONE: f32 = 1.0;
}

impl Float for f64 {
    const ZERO: f64 = 0.0;
    const ONE: f64 = 1.0;
}

fn fibonacci<T: Float + Add<Output=T>>(n: usize) -> T {
    match n {
        0 => T::ZERO,
        1 => T::ONE,
        n => fibonacci::<T>(n - 1) + fibonacci::<T>(n - 2),
    }
}

fn main() {
   let fibonacci_of_3: f32 = fibonacci::<f32>(3);
}

Playground 操场

Notice that you can not use dyn Float .请注意,您不能使用dyn Float The compiler do not know how to build the vtable for that trait, as it do not have any methods at all.编译器不知道如何为该特征构建 vtable,因为它根本没有任何方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rust:为关联类型实现特征“From”(错误) - Rust: Implement trait "From" for associated type (Error) 具有关联类型的特征对象中的 Rust 多态性 - Rust polymorphism in trait objects with associated types 在 Rust 中通过关联类型实现一个 trait 的问题 - Problem with implementing a trait over another trait with associated types in Rust 为什么 Rust 从特征到特征分配 const 不起作用? - Why does Rust assignment of const from trait to trait not work? 尽管有适当的特征绑定,但特征相关的 const 在特征定义上下文中不可用 - Trait associated const is not available in trait definition context despite appropriate trait bound Rust:当变量是引用时,特征推断不起作用 - Rust: Trait inference not working when variable is a reference 如何在Rust中定义关联的const或类型别名? - How can I define an associated const or type alias in Rust? 在 Rust nightly 中使用关联的常量和常量泛型时的类型不匹配 - Mismatched types when using associated consts and const generics in Rust nightly 为什么Rust中的const函数不能调用关联的函数? - Why can't const functions in Rust make calls to associated functions? 为什么Rust在实现具有关联类型的关联const时希望自己的大小? - Why does Rust want Self to be Sized when implementing an associated const with an associated type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM