首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法更新注册表:无法解析依赖项的服务器名称或地址

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

Stack Overflow用户
提问于 2017-08-12 19:51:16
回答 1查看 505关注 0票数 5

我正在使用Windows,并且面临下面的错误。不知道为什么它不能工作。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-08-12 20:12:49

您需要internet访问才能下载依赖项,如rand

尝试以下步骤:

代码语言:javascript
复制
cargo new guessing_game --bin
cd guessing_game
cargo build
cargo run

输出:

代码语言:javascript
复制
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!

编辑Cargo.toml文件:

代码语言:javascript
复制
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]

[dependencies]
rand = "0.3.14"

无法访问internet时的输出:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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

在main.rs中使用板条箱:

代码语言:javascript
复制
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;
            }
        }
    }
}

输出:

代码语言:javascript
复制
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!
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45649927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档