user5778069
user5778069

Reputation:

"Unable to update registry: The server name or address could not be resolved" for a dependency

I am using Windows and facing below error. Not sure why it's not working.

C:\RUST\guess_game>cargo build
    Updating registry `https://github.com/rust-lang/crates.io-index`
warning: spurious network error (2 tries remaining): [2/-1] failed to send request: The server name or address could not be resolved

warning: spurious network error (1 tries remaining): [2/-1] failed to send request: The server name or address could not be resolved

error: failed to load source for a dependency on `rand`

Caused by:
  Unable to update registry https://github.com/rust-lang/crates.io-index

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

Caused by:
  [2/-1] failed to send request: The server name or address could not be resolved

Upvotes: 5

Views: 943

Answers (1)

wasmup
wasmup

Reputation: 16273

You need internet access to download dependencies like rand:

Try these steps:

cargo new guessing_game --bin
cd guessing_game
cargo build
cargo run

Output:

C:\rust>cargo new guessing_game --bin
     Created binary (application) `guessing_game` project

C:\rust>cd guessing_game

C:\rust\guessing_game>cargo build
   Compiling guessing_game v0.1.0 (file:///C:/rust/guessing_game)
    Finished dev [unoptimized + debuginfo] target(s) in 0.34 secs

C:\rust\guessing_game>cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target\debug\guessing_game.exe`
Hello, world!

Edit the Cargo.toml file:

[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]

[dependencies]
rand = "0.3.14"

Output when there is no internet access:

C:\rust\guessing_game>cargo build
    Updating registry `https://github.com/rust-lang/crates.io-index`
warning: spurious network error (2 tries remaining): [2/-1] failed to send request: A connection with the serve
r could not be established

warning: spurious network error (1 tries remaining): [2/-1] failed to send request: A connection with the server could not be established

error: failed to load source for a dependency on `rand`

Caused by:
  Unable to update registry https://github.com/rust-lang/crates.io-index

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

Caused by:
  [2/-1] failed to send request: A connection with the server could not be established

Output with internet access. This takes some time and downloads 65MB of data for me:

C:\rust\guessing_game>cargo build
    Updating registry `https://github.com/rust-lang/crates.io-index`
 Downloading rand v0.3.16
 Downloading libc v0.2.29
   Compiling libc v0.2.29
   Compiling rand v0.3.16
   Compiling guessing_game v0.1.0 (file:///C:/rust/guessing_game)
    Finished dev [unoptimized + debuginfo] target(s) in 4.35 secs

Use the crate in main.rs:

extern crate rand;

use std::io;
use std::cmp::Ordering;
use rand::Rng;

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

    let secret_number = rand::thread_rng().gen_range(1, 101);

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

        let mut guess = String::new();

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

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        println!("You guessed: {}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }
}

Output:

C:\rust\guessing_game>cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target\debug\guessing_game.exe`
Guess the number!
Please input your guess.
50
You guessed: 50
Too small!
Please input your guess.
75
You guessed: 75
Too big!
Please input your guess.
60
You guessed: 60
Too big!
Please input your guess.
55
You guessed: 55
Too small!
Please input your guess.
57
You guessed: 57
You win!

Upvotes: 1

Related Questions