Compiler errors involving generic type parameters

Our tree structure requires that the data type used for the key has to have the PartialOrd and PartialEq traits. The &str type happens to have those traits, so we can use an &str for the key:

let mut tree: Tree<&'static str, f32> = Tree::new();

tree.set("first key", 12.65);
tree.set("second key", 99.999);
tree.set("third key", -128.5);
tree.set("fourth key", 67.21);

println!("tree.get_ref("third key") is {}", match tree.get_ref("third key") {
Err(_) => {println!("Invalid!"); &0.0},
Ok(x) => x,
});

Here, we've created a Tree<&'static str, f32>, or a tree that maps static strings to 32-bit floating point numbers. If we compile and run a complete program containing that snippet, everything works beautifully.

This data type, on the other hand, does not have the PartialOrd trait:

pub enum NotOrdered {
A,
B,
C,
}

If we substitute NotOrdered for &'static str as the key type for the tree, we suddenly get seven different compiler errors, which probably fill up the entire screen. Most of them look something like this:

This is telling us that the function was defined inside an implementation block that requires PartialOrd and PartialEq. Since our NotOrdered data type doesn't have those traits, the function we're trying to call doesn't exist, and the compiler is telling us that.

Up at the top of the list of errors, and possibly scrolled right off the screen, is a different error message:

This error message is somewhat more helpful than the other one, but it stems from the same cause. Our Tree data type requires a key type whose values can be compared to other values of the same type, and NotOrdered just doesn't provide that.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.61.81