为什么我得到“参数永远不会被使用[E0392]”?

时间:2015-08-17 14:47:53

标签: rust

我正在尝试在Rust中实现一个Octree。八叉树在具有约束的类型上是通用的,它应该实现一般特征:

export LD_LIBRARY_PATH=/path/to/codeSamples/myh/lib
./GLUI.out

这是simplified example reproducing the issue on the Playground

这会产生错误:

pub trait Generable<U> {
    fn generate_children(&self, data: &U) -> Vec<Option<Self>>;
}

pub enum Octree<T, U>
where
    T: Generable<U>,
{
    Node {
        data: T,
        children: Vec<Box<Octree<T, U>>>,
    },
    Empty,
    Uninitialized,
}

从签名中删除error[E0392]: parameter `U` is never used --> src/main.rs:5:20 | 5 | pub enum Octree<T, U> | ^ unused type parameter | = help: consider removing `U` or using a marker such as `std::marker::PhantomData` 会导致“未声明的类型名称'U'”。

我做错了什么或是错误吗?如何正确地做到这一点?

1 个答案:

答案 0 :(得分:7)

我不相信你想要另一个通用的you want an associated type

pub trait Generable {
    type From;
    fn generate_children(&self, data: &Self::From) -> Vec<Option<Self>>
    where
        Self: Sized;
}

pub enum Octree<T>
where
    T: Generable,
{
    Node {
        data: T,
        children: Vec<Box<Octree<T>>>,
    },
    Empty,
    Uninitialized,
}

fn main() {}

顺便说一下,Vec<Box<Octree<T>>>可能是间接的一个额外级别 - 你可以使用Vec<Octree<T>>