I have some code that works fine with exact types, but when I add generics it throws an error.
It gives me the following error, my code is below:
The trait
core::fmt::Display
is not implemented for the typeT
[E0277]
fn own<T>(x: T) -> T
{
x
}
struct Node<T>
{
value: T,
next: Option<Box<Node<T>>>
}
impl<T> Node<T>
{
fn print(&self)
{
let mut current = self;
loop {
println!("{}", current.value);
match current.next {
Some(ref next) => { current = &**next; },
None => break,
}
}
}
fn add(&mut self, node: Node<T>)
{
let item = Some(Box::new(node));
let mut current = self;
loop {
match own(current).next {
ref mut slot @ None => { *slot = item; return },
Some(ref mut next) => current = next
}
}
}
}
fn main() {
let leaf = Node { value: 10, next: None };
let branch = Node { value : 50, next: Some(Box::new(leaf)) };
let mut root = Node { value : 100, next: Some(Box::new(branch)) };
root.print();
let new_leaf = Node { value: 5, next: None };
root.add(new_leaf);
root.print();
}
I understand that this is a common error for generics in all languages, but when I try to add constraints to generics I get another error:
<anon>:12:8: 12:26 error: failed to resolve. Use of undeclared type or module core::fmt
<anon>:12 impl<T:core::fmt::Display> Node<T>
Why does it appear if I have copied the full qualified name from error and inserted it as a constraint?
It also doesn't work with another traits, for example impl<T:Num>
(Playground)