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