依赖项的“无法更新注册表:无法解析服务器名称或地址”

5 rust rust-cargo

我正在使用 Windows 并面临以下错误。不知道为什么它不起作用。

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
Run Code Online (Sandbox Code Playgroud)

was*_*mup 1

您需要访问互联网才能下载依赖项,例如rand

尝试以下步骤:

cargo new guessing_game --bin
cd guessing_game
cargo build
cargo run
Run Code Online (Sandbox Code Playgroud)

输出:

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!
Run Code Online (Sandbox Code Playgroud)

编辑 Cargo.toml 文件:

[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]

[dependencies]
rand = "0.3.14"
Run Code Online (Sandbox Code Playgroud)

无网络时的输出:

[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]

[dependencies]
rand = "0.3.14"
Run Code Online (Sandbox Code Playgroud)

具有互联网连接的输出。这需要一些时间并为我下载 65MB 的数据:

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
Run Code Online (Sandbox Code Playgroud)

使用 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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)