2

我有两个文件,loop.rs 包含一个函数请求,用于实例化客户端并获取网页正文。我想将请求导出到 main。我知道要导出我需要mod file_to_import然后use file_to_import::function_to_use根据这篇文章

src/
   main.rs
   loop.rs


// loop.rs ->
//crates
extern crate futures;
extern crate hyper;
extern crate tokio_core;
use std::io::{self, Write};
use self::futures::{Future, Stream};
use self::hyper::Client;
use self::tokio_core::reactor::Core;


//request function to be exported to main.rs

pub fn request(url: &str)  {
   let mut core = Core::new().unwrap();
   let client = Client::new(&core.handle());
   let uri = url.parse().unwrap();
   let work = client.get(uri).and_then(|res| {
      println!("Response: {}", res.status());

      res.body().for_each(|chunk| {
         io::stdout()
         .write_all(&chunk)
         .map_err(From::from)
     })
});
core.run(work).unwrap();
}


// main.rs ->
mod loop;
use loop::request;

fn main(){
   request("http://www.google.com");
}

在 main.rs 我想使用 request 但是当我构建它时出现以下错误

error: expected identifier, found keyword `loop`
 --> src/main.rs:1:5
  |
1 | mod loop;
  |     ^^^^ expected identifier, found keyword

error: expected identifier, found keyword `loop`
 --> src/server.rs:1:5
  |
1 | use loop::{request};
  |     ^^^^ expected identifier, found keyword

error: expected identifier, found keyword `loop`
 --> src/main.rs:4:5
  |
4 | use loop::*;
  |     ^^^^ expected identifier, found keyword
4

1 回答 1

8

loop是 Rust 中的关键字,这意味着它由解析器专门处理,不能用作标识符。

于 2018-01-30T23:58:38.080 回答