I have code that works fine with exact types, but when I add generics, it throws an error.
This gives me the following error: my code is below:
Feature core::fmt::Displaynot implemented for type T[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 mistake for generics in all languages, but when I try to add restrictions 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 this appear if I copied the fully qualified name from the error and pasted it as a constraint?
It also does not work with other signs, for exampleimpl<T:Num>
(Playground)
source
share