gpt4 book ai didi

导入时出现 Rust "expected identifier, found keyword"

转载 作者:行者123 更新时间:2023-11-29 08:22:46 28 4
gpt4 key购买 nike

我有两个文件,loop.rs 包含一个函数请求,用于实例化客户端并获取网页正文。我想将请求导出到 main.我知道要导出我需要 mod file_to_import 然后根据 this post 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

最佳答案

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

关于导入时出现 Rust "expected identifier, found keyword",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48532078/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com