#What are the reasons for using `extern crate foo` in `lib.rs`?

16 messages · Page 1 of 1 (latest)

errant coyote
#

sometimes, when you want to alias a crate in your code to something that makes more sense

i've used it for pem-rfc7468 before

extern crate pem-rfc7468 as pem;

use pem::Error;

instead of needing to write out the whole thing each time

proven niche
#

couple years ago you had to do it for every crate, it's not needed in the latest edition

dusty pewter
errant coyote
#

true, actually that + dep: prefixes probably eliminates all use cases for extern crate _ as _

#

so the only thing left is

#[macro_use]
extern crate log;
dusty pewter
#

You should be able to import those macros with use

#

The examples in log's docs all use use to import its macros.

errant coyote
#

i prefer that style for sure

#

but many people just want the less obvious info! crate-wide without needing to import

kindred karma
#

really the only current use for extern crate is to bring in alloc in a no_std crate

#

oh and aliasing the current crate for some macros

#

?play ```rs
extern crate self as demo;

fn main() {
demo::main()
}

rapid vaporBOT
#
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
timeout: the monitored command dumped core
/playground/tools/entrypoint.sh: line 15:     7 Aborted                 timeout --signal=KILL ${timeout} "$@"```
dusty pewter
dusty pewter
#

The only usecase I know for extern crate self as blargh; is
to define a frontend crate that internally uses the proc macro that it re-exports.