Does a crate prelude only helps to avoid writing extern crate every module?

I read the extern crate prelude section; which says declare extern crate xxx in the root module will add the crate to prelude.

MY question is what is the effect of prelude?
I notice I also need to use xxx::some_path::* to import the symbols to the module for use.

Since 2018, you don't need to write extern crate xxx; unless you're dealing with some no_std code.

externe crate xxx justs add xxx to the defaults imports (the prelude) in your crate, so that xxx will be available by default in all your modules.

It's rarely necessary nowadays

But useful when you want to rename a crate, or you have different crates with the same interface which are chosen by a compile flag. Such as std and a mockstd.

So only use if enough? however, the result is not OK

zylthinking@linux:~/code/rust-comn/tests$ cat 
//extern crate comn;
use comn;

fn main() {
    let s: *const String = unsafe { comn::malloc() };
zylthinking@linux:~/code/rust-comn/tests$ cargo build && rustc -L ../target/debug/deps --extern comn=../target/debug/libcomn.rlib 
warning: unnecessary lifetime parameter `'a`
  --> src/
48 | pub fn declare_argment<'a: 'static>(
   |                        ^^^^^^^^^^^
   = help: you can use the `'static` lifetime directly, in place of `'a`

warning: `comn` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
error[E0432]: unresolved import `comn`
2 | use comn;
  |     ^^^^ no `comn` in the root

error: aborting due to previous error

If you invoke rustc directly (which is not recommended in general), you should pass the --edition parameter, to use the features from editions other then 2018.

1 Like

Ok, I can delete extern crate xxx in 2021 edition;
While the origin question "how does a prelude take effect" is not resolved.

If I write extern crate xxx in the root module, what will it bring to me?

Nothing, it's just a redundant information.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.