Failed to resolve: use of undeclared crate or module `average_block_sizeproc_macro`

I've started learning Rust and am following this guide: docs, but encountered this error at this step:

https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html#using-a-crate-to-get-more-functionality

Before I added this code to my Cargo.toml, everything was fine:

[dependencies]
rand = "0.8.5"

But after adding this line, I encountered an error:

~/g/rust-by-example/02/guessing_game main ❯ cargo build                                                              
warning: `/Users/smallyu/.cargo/config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
warning: `/Users/smallyu/.cargo/config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
   Compiling proc-macro2 v1.0.86
error[E0433]: failed to resolve: use of undeclared crate or module `average_block_sizeproc_macro`
   --> /Users/smallyu/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.86/src/wrapper.rs:871:21
    |
871 |                     average_block_sizeproc_macro::Literal::byte_character(byte)
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `average_block_sizeproc_macro`
    |
help: consider importing one of these structs
    |
1   + use crate::Literal;
    |
1   + use crate::fallback::Literal;
    |
1   + use proc_macro::Literal;
    |
help: if you import `Literal`, refer to it directly
    |
871 -                     average_block_sizeproc_macro::Literal::byte_character(byte)
871 +                     Literal::byte_character(byte)
    |

For more information about this error, try `rustc --explain E0433`.
error: could not compile `proc-macro2` (lib) due to 1 previous error

I'm on macOS, and the Rust version is:

~/g/rust-by-example/02/guessing_game main ❯ cargo --version                                                          smallyunet
warning: `/Users/smallyu/.cargo/config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
warning: `/Users/smallyu/.cargo/config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
cargo 1.82.0-nightly (0d8d22f83 2024-08-08)
~/g/rust-by-example/02/guessing_game main ❯ rustc --version                                                          smallyunet
rustc 1.82.0-nightly (91376f416 2024-08-12)

My full code:

use std::io;

fn main() {
    println!("Guess the number!");

    println!("Please input your guess.");

    let mut guess = String::new();

    io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");

    println!("You guessed: {}", guess);
}
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8.5"

It seems that you, or some program running on your computer, has corrupted the downloaded sources in ~/.cargo/registry that should never be modified. (You can see the correct line here.)

To avoid any problems from changes that aren't causing compilation failure, I recommend deleting /Users/smallyu/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.86/ or even /Users/smallyu/.cargo/registry/ — Cargo will re-download everything it needs there automatically.

That should solve your problem, but there are also two weird things I notice:

  • Why do you have a global Cargo config file /Users/smallyu/.cargo/config? The warning is just suggesting that you rename it, but as a beginner, you shouldn't need one at all. What's in that file? Do you know how it came to exist?
  • Why are you using nightly instead of stable Rust? They're not greatly different, but using nightly exposes you to more risk of bugs and weird behaviors.

Perhaps you have a reason for these things, but if you don't, it'd be better to avoid them.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.