如何在一系列通用可变选项上实现复制和克隆特征

问题描述

我正在尝试创建一个特里树,但是在使索引可变时遇到了一个问题。我认为[None; 26]是不可复制的,也就是说我引用的是临时性内容。

pub enum Result<T> {
    Ok,KeyExists(T),}

pub struct Trie<'a,T: Copy> {
    root: &'a Node<'a,T>,}

impl<'a,T: Copy> Trie<'a,T> {
    pub fn new() -> Trie<'a,T> {
        Trie {
            root: &Node {
                next: [None; 26],// Compilation Error Here
                data: None,},}
    }
}

impl<'a,T: Copy> From<Vec<String>> for Trie<'a,T> {
    fn from(a: Vec<String>) -> Self {
        // Vec::new()
        Trie::new()
    }
}

struct Node<'b,T: Copy> {
    next: [Option<&'b mut Node<'b,T>>; 26],data: Option<T>,}

impl<T: Copy> Copy for Option<T> {}

impl<T: Copy> Clone for Option<T> {
    fn clone(&self) -> Self {
        *self
    }
}

当我尝试分配[None:26]时出现编译错误。

error[E0119]: conflicting implementations of trait `std::clone::Clone` for type `std::option::Option<_>`:
  --> src/lib.rs:35:1
   |
35 | impl<T: Copy> Clone for Option<T> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> std::clone::Clone for std::option::Option<T>
             where T: std::clone::Clone;

error[E0119]: conflicting implementations of trait `std::marker::Copy` for type `std::option::Option<_>`:
  --> src/lib.rs:33:1
   |
33 | impl<T: Copy> Copy for Option<T> {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> std::marker::Copy for std::option::Option<T>
             where T: std::marker::Copy;

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> src/lib.rs:33:1
   |
33 | impl<T: Copy> Copy for Option<T> {}
   | ^^^^^^^^^^^^^^^^^^^^^^^---------
   | |                      |
   | |                      `std::option::Option` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   |
   = note: define and implement a trait or new type instead

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> src/lib.rs:35:1
   |
35 | impl<T: Copy> Clone for Option<T> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^---------
   | |                       |
   | |                       `std::option::Option` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   |
   = note: define and implement a trait or new type instead

我认为我需要实现Copy / Clone,但是我尝试了各种不同的方法,但不确定如何使它起作用。这是我尝试过的例子。

impl<T: Copy> Copy for Option<T> {}

impl<T: Copy> Clone for Option<T> {
    fn clone(&self) -> Self {
        *self
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)