Skip to content

Commit d170748

Browse files
committed
RFC: Finalizing more naming conventions
1 parent 9bf2874 commit d170748

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
- Start Date: 2014-11-02
2+
- RFC PR: (leave this empty)
3+
- Rust Issue: (leave this empty)
4+
5+
# Summary
6+
7+
This conventions RFC tweaks and finalizes a few long-running de facto
8+
conventions, including capitalization/underscores, and the role of the `unwrap` method.
9+
10+
# Motivation
11+
12+
This is part of the ongoing conventions formalization process. The
13+
conventions described here have been loosely followed for a long time,
14+
but this RFC seeks to nail down a few final details and make them
15+
official.
16+
17+
# Detailed design
18+
19+
## General naming conventions
20+
21+
In general, Rust tends to use `CamelCase` for "type-level" constructs
22+
(types and traits) and `snake_case` for "value-level" constructs. More
23+
precisely, the proposed (and mostly followed) conventions are:
24+
25+
| Item | Convention |
26+
| ---- | ---------- |
27+
| Crates | `snake_case` (but prefer single word) |
28+
| Modules | `snake_case` |
29+
| Types | `CamelCase` |
30+
| Traits | `CamelCase` |
31+
| Enum variants | `CamelCase` |
32+
| Functions | `snake_case` |
33+
| Methods | `snake_case` |
34+
| General constructors | `new` or `new_with_more_details` |
35+
| Conversion constructors | `from_some_other_type` |
36+
| Local variables | `snake_case` |
37+
| Static variables | `SCREAMING_SNAKE_CASE` |
38+
| Type parameters | concise `CamelCase`, usually single uppercase letter: `T` |
39+
| Lifetimes | short, lowercase: `'a` |
40+
41+
### Fine points
42+
43+
In `CamelCase`, acronyms count as one word: use `Uuid` rather than `UUID`.
44+
45+
In `snake_case` or `SCREAMING_SNAKE_CASE`, a "word" should never
46+
consist of a single letter unless it is the last "word". So, we have
47+
`btree_map` rather than `b_tree_map`, but `PI_2` rather than `PI2`.
48+
49+
## `unwrap`, `into_foo` and `into_inner`
50+
51+
There has been a [long](https://github.com/mozilla/rust/issues/13159)
52+
[running](https://github.com/rust-lang/rust/pull/16436)
53+
[debate](https://github.com/rust-lang/rust/pull/16436)
54+
[about](https://github.com/rust-lang/rfcs/pull/328) the name of the
55+
`unwrap` method found in `Option` and `Result`, but also a few other
56+
standard library types. Part of the problem is that for some types
57+
(e.g. `BufferedReader`), `unwrap` will never panic; but for `Option`
58+
and `Result` calling `unwrap` is akin to asserting that the value is
59+
`Some`/`Ok`.
60+
61+
There's basic agreement that we should have an unambiguous term for
62+
the `Option`/`Result` version of `unwrap`. Proposals have included
63+
`assert`, `ensure`, `expect`, `unwrap_or_panic` and others; see the
64+
links above for extensive discussion. No clear consensus has emerged.
65+
66+
This RFC proposes a simple way out: continue to call the methods
67+
`unwrap` for `Option` and `Result`, and rename *other* uses of
68+
`unwrap` to follow conversion conventions. Whenever possible, these
69+
panic-free unwrapping operations should be `into_foo` for some
70+
concrete `foo`, but for generic types like `RefCell` the name
71+
`into_inner` will suffice. By convention, these `into_` methods cannot
72+
panic; and by (proposed) convention, `unwrap` should be reserved for
73+
an `into_inner` conversion that *can*.
74+
75+
# Drawbacks
76+
77+
Not really applicable; we need to finalize these conventions.
78+
79+
# Unresolved questions
80+
81+
Are there remaining subtleties about the rules here that should be clarified?

0 commit comments

Comments
 (0)