我有两个文件,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