Search code examples
rustrust-actix

Why can actix_rt be used directly in the code, even if it wasn't declared?


I just added actix_rt in Cargo.toml and didn't declare it at the first line with the use keyword. Then I could use it in the code. I know some frequently used functions are included in the prelude of Rust, but I had no idea 3rd party libraries could do the some thing. Can I create a crate like that?

Any one could tell me why or give me some tips or some reference links? I'd appreciate it.

[dependencies]
actix-rt = "0.2.5"
actix-web = "1.0.8"
use std::io;

fn main() -> io::Result<()> {
    let sys = actix_rt::System::new("basic");

    sys.run()
}

Solution

  • In the Rust 2018 Edition, extern crate is no longer required. Putting a crate as a dependency allows it to be accessed as a module. There's nothing you need to do to make your crate accessible like this.

    This is very different from the standard library prelude, which uses all the items in the prelude implicitly (with use std::prelude::v1::*;). With extern crate or adding an external crate as a dependency, the types, functions and traits have to be qualified. In your example, you have to use actix_rt::System::new("basic") rather than simply System::new("basic"). Compare this to std::prelude::v1::Option, which can be used as Option<T> without any prefix.