Skip to content

Commit af7485c

Browse files
committed
RFC: Boil Down Externs
Dearest Reviewer, I hope all is well. I have been enjoying rust immensely. Recently I was working on converting an old ruby project to rust. I was adding my 20ish crate to my project and after scrolling down I thought, "Do I need all the `extern crate` lines?". I set out by making a macro, my first, which was easy and fun. I posted my macro to reddit wondering if I had missed something. After many great comments someone suggested an rfc with a wink face[1](https://www.reddit.com/r/rust/comments/5q7ssv/long_list_of_externs/). I figured why not. I wrote up this start and then went over to [internals.rust-lang.org](https://internals.rust-lang.org/t/pre-rfc-boil-down-externs/4676). Here I received advice on syntax and missing cases to my rfc. Excellent feedback again! I hope its considered to allow one line of `extern crate` to accept many crates. I understand completely if it is not. Thanks again Becker Updated with alt-syntax
1 parent 849ca2f commit af7485c

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

text/0000-boil-down-externs.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
- Feature Name: boil_down_externs
2+
- Start Date: 2017-01-26
3+
- RFC PR: (leave this empty)
4+
- Rust Issue: (leave this empty)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
This RFC proposes to change the current `extern crate` syntax to allow
10+
for multiple crates to be listed.
11+
12+
# Motivation
13+
[motivation]: #motivation
14+
15+
This proposes will improve the ergonomics of the Rust language for
16+
projects that have many dependencies.
17+
18+
# Detailed design
19+
[design]: #detailed-design
20+
21+
Implementation would take the current `extern crate` but allow a list of
22+
crates instead of a single crate. A single create would just be a single
23+
list item.
24+
25+
Example:
26+
```
27+
extern crate {
28+
rocket, rocket_contrib, serde_json as json, chrono,
29+
dotenv, postgres, r2d2, r2d2_diesel, tera as template,
30+
serde_derive, toml, glob
31+
};
32+
```
33+
34+
```
35+
pub extern crate {rocket, rocket_contrib};
36+
```
37+
38+
Meta items before the `extern` ie. `#[macro_use]` would be applied to
39+
all crates listed.
40+
41+
42+
Example:
43+
```
44+
#[macro_use]
45+
extern crate {diesel, diesel_codegen, lazy_static, serde_derive};
46+
```
47+
48+
49+
Alternatively no braces
50+
51+
52+
```
53+
extern crate rocket, rocket_contrib, serde_json as json, chrono,
54+
dotenv, postgres, r2d2, r2d2_diesel, tera as template,
55+
serde_derive, toml, glob;
56+
```
57+
58+
59+
Duplicate externs would error like it does currently.
60+
61+
# How We Teach This
62+
[how-we-teach-this]: #how-we-teach-this
63+
64+
No new names or terminology needed to teach this. Examples should be
65+
updated to include the list syntax.
66+
67+
Examples in both books, The Rust Programming Language and Rust by
68+
Example, should be updated where more then one crate is used. A small
69+
section should be added to http://rustbyexample.com/crates.html
70+
71+
rustfmt should have an opinion how to format the list of crates as well.
72+
73+
# Drawbacks
74+
[drawbacks]: #drawbacks
75+
76+
1. Adds multiple ways to do things to a language.
77+
2. Diffs can mask changes in lists.
78+
3. Would be replaced buy future improvements to crate ecosystem
79+
4. Differing opinions on `extern crate` and it's relationship to `mod`
80+
81+
# Alternatives
82+
[alternatives]: #alternatives
83+
84+
A crate could be release with a macro. Like the example but one that supports meta values.
85+
86+
```
87+
macro_rules! externs {
88+
( $( $x:ident ),* ) => {
89+
$(
90+
extern crate $x;
91+
)*
92+
};
93+
}
94+
externs![rocket, rocket_contrib, serde_json]
95+
```
96+
97+
Supporting meta values could be an issue with the macro.
98+
Recursion could also be an issue. [Details discussed
99+
here](https://botbot.me/mozilla/rust-internals/2017-01-29/?msg=80105364&page=1)
100+
101+
# Unresolved questions
102+

0 commit comments

Comments
 (0)