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