Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Lib crate isn't recognized #686

@Joelius300

Description

@Joelius300

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions