0

I'm trying to create a trie and I'm running into an issue when it comes to turning my index mutable. I think [None; 26] is not copyable so it's saying I have a reference to something that's temporary.

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
    }
}

I get a compilation error when I tried to assign [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

I think I need to implement Copy / Clone but I tried a variety of different things and I wasn't sure how to get it to work. Here's an example of something I tried.

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

impl<T: Copy> Clone for Option<T> {
    fn clone(&self) -> Self {
        *self
    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Luke Xu
  • 2,302
  • 3
  • 19
  • 43
  • 1
    There is a lot wrong with your approach: 1. `fn new() -> Trie<'a, T>` cannot work because there would be no owner. 2. copying a mutable reference cannot work because it would defeat one of the fundamental guarantee of the borrow checker: there is at most one mutable reference to an object. 3. You cannot implement a trait you don't own for a type you don't own, which is known as the orphan rule. – mcarton Sep 23 '20 at 00:04
  • Those kinds of graphs and linked list are _hard_ to implement in Rust. You should get more experience with the language before you try to implement them. – mcarton Sep 23 '20 at 00:05

0 Answers0