diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2021-07-04 11:12:41 +0300 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2021-07-04 12:48:29 +0300 |
| commit | 3be9ebe2c3726c7efc77ebdddb4445f7aff4260e (patch) | |
| tree | af7bc066d95cd2bdc17e8be1737b17629990d7bb /docs/dev | |
| parent | 90e27d6289d8959160d2334cb21993f7ed1a24af (diff) | |
| download | rust-3be9ebe2c3726c7efc77ebdddb4445f7aff4260e.tar.gz rust-3be9ebe2c3726c7efc77ebdddb4445f7aff4260e.zip | |
minor: style
Diffstat (limited to 'docs/dev')
| -rw-r--r-- | docs/dev/style.md | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index 84485ea2845..f56f36e71c9 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -891,7 +891,6 @@ let buf = { }; // BAD - let buf = prepare_buf(&mut arena, item); ... @@ -909,6 +908,37 @@ Exception: if you want to make use of `return` or `?`. A block serves just as well to delineate a bit of logic, but has access to all the context. Re-using originally single-purpose function often leads to bad coupling. +## Local Helper Functions + +Put nested helper functions at the end of the enclosing functions +(this requires using return statement). +Don't nest more than one level deep. + +```rust +// GOOD +fn dfs(graph: &Graph, v: Vertex) -> usize { + let mut visited = FxHashSet::default(); + return go(graph, &mut visited, v); + + fn go(graph: &Graph, visited: &mut FxHashSet<Vertex>, v: usize) -> usize { + ... + } +} + +// BAD +fn dfs(graph: &Graph, v: Vertex) -> usize { + fn go(graph: &Graph, visited: &mut FxHashSet<Vertex>, v: usize) -> usize { + ... + } + + let mut visited = FxHashSet::default(); + go(graph, &mut visited, v) +} + +``` + +**Rationale:** consistency, improved top-down readability. + ## Helper Variables Introduce helper variables freely, especially for multiline conditions: |
