Should I use a alternative to .cmp(), if so what would be the appropiate tool. I can't seem to figure this out.
Code
use std::cmp::Ordering;
struct Painting {
artist: String,
title: String,
current_price: f32,
}
struct Car {
brand: String,
model: String,
original_price: f32,
age: u8,
}
pub trait Valueable {
fn get_price(self) -> f32;
fn get_description(self) -> String;
}
impl Valueable for Car {
fn get_price(self) -> f32 {
return self.original_price * 0.95_f32.powf(self.age.into());
}
fn get_description(self) -> String {
return self.model;
}
}
impl Valueable for Painting {
fn get_price(self) -> f32 {
return self.current_price;
}
fn get_description(self) -> String {
return self.title;
}
}
pub enum BST {
Leaf {
value: Box<dyn Valueable>,
left: Box<BST>,
right: Box<BST>,
},
Empty,
}
impl BST {
pub fn new() -> Self {
BST::Empty
}
// TODO: Replace .cmp() with a different comparison method potentially
pub fn add(&mut self, new_value: Box<dyn Valueable>) {
match self {
BST::Leaf {
value,
left,
right,
} => match new_value.get_price().cmp(value.get_price()) {
// Tried: Original: match new_value.cmp(value)
// Tried: match new_value.get_price().cmp(&value.get_price()
// Tried: new_value.get_price() == value.get_price()
Ordering::Less => left.add(new_value),
Ordering::Greater => right.add(new_value),
Ordering::Equal => return,
},
BST::Empty => {
*self = BST::Leaf {
value: new_value,
left: Box::new(BST::Empty),
right: Box::new(BST::Empty),
}
}
}
}
pub fn contains(&self, find_value: Box<dyn Valueable>) -> bool {
match self {
BST::Leaf {
value,
left,
right,
} => match find_value.cmp(value) {
Ordering::Less => left.contains(find_value),
Ordering::Greater => right.contains(find_value),
Ordering::Equal => true,
},
BST::Empty => false,
}
}
pub fn as_vector(&mut self) -> Vec::<String> {
match self {
BST::Empty => return Vec::new(),
BST::Leaf { value, left ,right } => {
let mut values = left.as_vector();
values.push(value.clone());
values.extend(right.as_vector());
values
},
}
}
}
fn main(){
let mut bst = BST::new();
let car1 = Car {
brand: "Volvo".to_string(),
model: "123ABC".to_string(),
original_price: 23000.50,
age: 12,
};
BST::add(&mut bst, Box::new(car1));
let painting1 = Painting {
artist: "Hans".to_string(),
title: "Hanswacht".to_string(),
current_price: 999.33,
};
}
Compiler output
error[E0599]: `f32` is not an iterator
--> main.rs:66:46
|
66 | } => match new_value.get_price().cmp(value.get_price()) {
| ^^^ `f32` is not an iterator
|
= note: the following trait bounds were not satisfied:
`f32: Iterator`
which is required by `&mut f32: Iterator`
error[E0599]: the method `cmp` exists for struct `Box<(dyn Valueable + 'static)>`, but its trait bounds were not satisfied
--> main.rs:91:35
|
17 | pub trait Valueable {
| ------------------- doesn't satisfy `(dyn Valueable + 'static): Iterator`, `dyn Valueable: Iterator` or `dyn Valueable: Ord`
...
91 | } => match find_value.cmp(value) {
| ^^^ method cannot be called on `Box<(dyn Valueable + 'static)>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`dyn Valueable: Iterator`
which is required by `Box<dyn Valueable>: Iterator`
`dyn Valueable: Ord`
which is required by `Box<dyn Valueable>: Ord`
`Box<(dyn Valueable + 'static)>: Iterator`
which is required by `&mut Box<(dyn Valueable + 'static)>: Iterator`
`(dyn Valueable + 'static): Iterator`
which is required by `&mut (dyn Valueable + 'static): Iterator`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following traits define an item `cmp`, perhaps you need to implement one of them:
candidate #1: `Ord`
= note: the trait `Iterator` defines an item `cmp`, but is explicitly unimplemented
error[E0599]: the method `clone` exists for mutable reference `&mut Box<(dyn Valueable + 'static)>`, but its trait bounds were not satisfied
--> main.rs:106:35
|
17 | pub trait Valueable {
| ------------------- doesn't satisfy `dyn Valueable: Clone` or `dyn Valueable: Sized`
...
106 | values.push(value.clone());
| ^^^^^ method cannot be called on `&mut Box<(dyn Valueable + 'static)>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`dyn Valueable: Sized`
which is required by `Box<dyn Valueable>: Clone`
`dyn Valueable: Clone`
which is required by `Box<dyn Valueable>: Clone`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the trait `Clone` defines an item `clone`, but is explicitly unimplemented
warning: unused variable: `painting1`
--> main.rs:126:9
|
126 | let painting1 = Painting {
| ^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_painting1`
|
= note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default
For more information about this error, try `rustc --explain E0599`.
warning: `src` (bin "src") generated 1 warning
error: could not compile `src` (bin "src") due to 3 previous errors; 1 warning emitted