zep
zep

Reputation: 311

Rust "expected identifier, found keyword" on importing

I have two files, loop.rs contains a function request to instantiate a client and get a body of a webpage. I want to export request to main. I know that to export I need to mod file_to_import and then use file_to_import::function_to_use according to 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");
}

In main.rs I want to use request but when I build this I get the following errors

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

Upvotes: 2

Views: 9866

Answers (1)

mbrubeck
mbrubeck

Reputation: 969

loop is a keyword in Rust, which means it is handled specially by the parser and can't be used as an identifier.

Upvotes: 8

Related Questions