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.
Hyeonu
November 25, 2021, 8:16am
#2
Since 2018, you don't need to write extern crate xxx;
unless you're dealing with some no_std code.
erelde
November 25, 2021, 8:17am
#3
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 use.rs
//extern crate comn;
use comn;
fn main() {
let s: *const String = unsafe { comn::malloc() };
}
zylthinking@linux:~/code/rust-comn/tests$ cargo build && rustc use.rs -L ../target/debug/deps --extern comn=../target/debug/libcomn.rlib
warning: unnecessary lifetime parameter `'a`
--> src/flags.rs:48:24
|
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`
--> use.rs:2:5
|
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?
Hyeonu
November 25, 2021, 9:28am
#8
Nothing, it's just a redundant information.
system
closed
February 23, 2022, 9:28am
#9
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.