Good morning!
I'm a formerly seasoned programmer with experience in Pascal, C, C++, Java, perl, and some python; however, most of my dev experience is from the late '90s to early 2000s (hence the languages you see). I'm trying to learn Rust but can't seem to understand why I can't call a function declared within my module from another function within the module. Ignore the use of print!() vice println!() inside the print_* functions because they actually contain long multiline strings rather than the dummy strings shown in this paste.
Here's the code at issue that is generating errors I can't resolve:
// module for parsing cmdline options
use std::collections::HashMap;
pub fn parse_options(argv: Vec) -> HashMap<String, String> {
return options::parse_options(argv);
}
mod options {
use std::collections::HashMap;
use std::env;
use std::io;
use std::process;
const help_opt: String = "--help".to_string();
const help_opt_short: String = "-h".to_string();
pub fn parse_options(argv: Vec<String>) -> HashMap<String, String> {
let parsed = HashMap::new();
// state machine for parsing arguments
// state 0: -- if no options, print short help
if (argv.len() == 0) {
options::print_usage_synopsis();
process::exit(0);
}
// state 1: -- check if --help passed anywhere
else if (argv.contains(&help_opt) || argv.contains(&help_opt_short)) {
options::print_full_help();
process::exit(0);
}
return parsed;
}
fn print_usage_synposis() {
print!("usage_synopsis\n");
}
fn print_full_help() {
print!("full_help\n");
}
}
In my main.rs, I do the following:
use std::collections::HashMap;
use std::env;
use std::io;
use std::process;
pub mod options;
fn main() {
let argv: Vec = env::args().collect();
println!("foo v0.0");
println!("args passed were: {:?}", argv);
options::parse_options(argv);
process::exit(0);
}
My src directory structure is as follows:
src/
|
+----main.rs
|
+----options.rs
Running cargo build results in the following:
error[E0433]: failed to resolve: use of undeclared crate or module `options`
--> src/options.rs:25:13
|
25 | options::print_usage_synopsis();
| ^^^^^^^
| |
| use of undeclared crate or module `options`
| help: an enum with a similar name exists: `Option`
error[E0433]: failed to resolve: use of undeclared crate or module
options
--> src/options.rs:30:13
|
30 | options::print_full_help();
| ^^^^^^^
| |
| use of undeclared crate or module options
| help: an enum with a similar name exists: Option
I have tried calling the print_* functions with no namespace qualifiers, with the options:: namespace, the options::options:: namespace, and with the crate::options namespace. Nothing works. What am I missing here?
FYSA, namespaces are "new" to me -- I learned programming on my own from Basic then Pascal in high school and C, C++, and Java in college from the early-mid 1990s and then Perl and Python in the early 2000s (i.e., before 2005). So I'm familiar with the namespaces concept as far as multi-module C and C++ code before the dawn of the "namespaces" as a concept that you had to declare separately from class definitions themselves.
Thank you and have a great day!
--Jason