How do I get around there being no function overloading?
I'm writing a binary tree and I want a function to initialize an empty tree and one to initialize one with a head value. I tried to write two separate new functions but then I learned that Rust does not actually have overloading. Here's my empty tree function:
pub fn new<T: Ord>() -> BST<T> {
return BST {
size: 0,
head: None,
};
}
I tried to write a pub fn new<T: Ord>(first: T) -> BST<T> { ... } function, but then I was arrested by the compiler police for trying to overload a function. How do I write something similar? Do I need two separate function names or is there a way to do this with traits?