Guessing game fails to build on wsl2

Hello!

I'm using wsl2 to try and learn Rust. I'm reading through the rust lang book and the infamous second chapter is hampering my progress. I've set up my dependancies as:

[package]
name = "guessing_game"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.3"

I am trying to run:

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);
}

However, I end up with an error:

     Updating crates.io index
error: failed to get `rand` as a dependency of package `guessing_game v0.1.0 (path/to/guessing_game)`

Caused by:
  failed to load source for dependency `rand`

Caused by:
  Unable to update registry `crates-io`

Caused by:
  failed to fetch `https://github.com/rust-lang/crates.io-index`

Caused by:
  network failure seems to have happened
  if a proxy or similar is necessary `net.git-fetch-with-cli` may help here
  https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli

Caused by:
  SSL error: received early EOF; class=Ssl (16); code=Eof (-20)

I would like to continue using wsl2 to learn rust, does anyone have a suggestion? Thanks!

It looks like the WSL2 container does not have internet access.

I found a couple of discussions about possible solutions from a Google search, including possible antivirus interference, perhaps you can figure out why the WSL2 distro does not have internet access?

I have pinged users.rust-lang.org from the terminal and it gives valid responses (AFAIK, I'm no network wizard):

ping users.rust-lang.org
PING rust-lang.hosted-by-discourse.com (64.71.144.205) 56(84) bytes of data.
64 bytes from 64.71.144.205 (64.71.144.205): icmp_seq=1 ttl=53 time=165 ms
...
5 packets transmitted, 5 received, 0% packet loss, time 4

I'll have a look into whether wsl2 is blocked by the antivirus.

I ended up managing to fix it!

I ended up making a config in the cargo folder present in the $HOME directory. If you don't have one, make one.

$HOME/.cargo/config.toml (Doesn't need a .toml suffix in later versions of rust, >1.31)

Inside the config.toml I put:

[net] 
retry = 3
git-fetch-with-cli = true 

I ended up copying the above from some config file in the cargo book, I think. I tried running cargo build from the guessing_game directory but it failed!

I then found a solution for the error returned on stackoverflow which said to run:

sudo apt install gnutls-bin

After which, I was able to download the rand package!

My guess is that Rust is trying to download stuff in it's own codebase instead of just using git. Force your system to use git and you should be good to go.

It's easy when you know how it went wrong!

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.