I am learning Rust and trying to figure out why I can't call a method from a module I have written. The code is very simple as it's a learning exercise and I have read this and this but I still don't know what the problem is. I have also tried using the code from this question almost exactly however the error persists.
The code for main.rs and helper.rs are below.
main.rs:
mod helpers;
fn main() {
let h = helpers::new("TestString");
println!("{}", h.get_name().to_string());
helpers::test_func();
}
helpers.rs
struct Helper{
name: String
}
impl Helper {
pub fn new(name: &String) -> Helper {
let helper = Helper{ name: name.to_string()};
return helper;
}
}
impl Helper {
pub fn get_name(&self) -> &String {
return &self.name;
}
}
pub fn test_func(){
println!("test_func was called.");
}
The error I am getting is:
error[E0425]: cannot find function `new` in module `helpers`
--> src/main.rs:4:30
|
4 | let h = helpers::new("TestString");
| ^^^ not found in `helpers`
error: aborting due to previous error
I know the file is being read because if you commend out lines 4 & 5 from main.rs and just run helpers::test_func()
it works fine however I don't know why it can't find the new method.
Any help would be very much appreciated.