-
Notifications
You must be signed in to change notification settings - Fork 161
Lib crate isn't recognized #686
Description
I'm currently working through the book and found what seems to be a bug in the VS Code extension. I'm in part 3 of building this cli-application and have just moved some logic from main.rs
to lib.rs
. Just like it says in the book, I used use minigrep::Config
and minigrep::run
to refer to the stuff defined in lib.rs
. It works perfectly fine if I build/run it with cargo but VS Code shows the following errors:
For use minigrep::Config;
unresolved import `minigrep`
use of undeclared type or module `minigrep` rustc(E0432)
main.rs(4, 5): use of undeclared type or module `minigrep`
For minigrep::run(config)
:
failed to resolve: use of undeclared type or module `minigrep`
use of undeclared type or module `minigrep` rustc(E0433)
main.rs(16, 21): use of undeclared type or module `minigrep`
Here's the code in main.rs
:
use std::env;
use std::process;
use minigrep::Config;
fn main() {
let args: Vec<String> = env::args().collect();
let config = Config::new(&args).unwrap_or_else(|err| {
println!("Problem parsing arguments: {}", err);
process::exit(1);
});
println!("Searching for {}", config.query);
println!("In file {}", config.filename);
if let Err(e) = minigrep::run(config) {
println!("Application error: {}", e);
process::exit(1);
}
}
Probably unnecessary but here's lib.rs
:
use std::fs;
use std::error::Error;
pub struct Config {
pub query: String,
pub filename: String
}
impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
if args.len() < 3 {
return Err("Not enough arguments")
}
let query = args[1].clone();
let filename = args[2].clone();
Ok(Config { query, filename })
}
}
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.filename)?;
println!("With text:\n{}", contents);
Ok(())
}
Again, building and running it with cargo works perfectly fine and the package name defined in Cargo.toml
is minigrep
, just like it's supposed to be. In case it's not obvious, it was all done step-by-step following the book.