导入时生锈“预期标识符,找到关键字”

时间:2018-01-30 23:20:36

标签: rust hyper

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

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中我想使用请求,但是当我构建它时,我得到以下错误

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

1 个答案:

答案 0 :(得分:5)

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