I'm just started to learn Rust and I'm wondering if there is way to overload methods. At first I created a struct and used a 'impl' to implement basic 'new' method. Then I thought to add 'new' method with some params, and I tried to use trait for that.
The following code was successfully compiled but once I tried to use 'new' with params, compiler gave me an error about extra params. So how should I overload methods in Rust?
pub struct Words<'a> {
pub nouns: Vec<&'a str>,
}
trait Test<'a>{
fn new(nouns: Vec<&'a str>) -> Self;
}
impl<'a> Words<'a> {
pub fn new() -> Words<'a>{
let nouns = vec!["test1", "test2", "test3", "test4"];
Words{ nouns: nouns }
}
pub fn print(&self){
for i in self.nouns.iter(){
print!("{} ", i);
}
}
}
impl<'a> Test<'a> for Words<'a> {
fn new(nouns: Vec<&'a str>) -> Words<'a>{
Words{ nouns: nouns }
}
}