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
    }
}
2
  • 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. Commented Sep 23, 2020 at 0: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. Commented Sep 23, 2020 at 0:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.