summary refs log tree commit diff
path: root/src/doc/style/features/functions-and-methods/output.md
blob: 3e43d1e416d7696759d37a7dae545655fb951810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
% Output from functions and methods

### Don't overpromise. [FIXME]

> **[FIXME]** Add discussion of overly-specific return types,
> e.g. returning a compound iterator type rather than hiding it behind
> a use of newtype.

### Let clients choose what to throw away. [FIXME: needs RFC]

#### Return useful intermediate results:

Many functions that answer a question also compute interesting related data.  If
this data is potentially of interest to the client, consider exposing it in the
API.

Prefer

```rust
struct SearchResult {
    found: bool,          // item in container?
    expected_index: usize // what would the item's index be?
}

fn binary_search(&self, k: Key) -> SearchResult
```
or

```rust
fn binary_search(&self, k: Key) -> (bool, usize)
```

over

```rust
fn binary_search(&self, k: Key) -> bool
```

#### Yield back ownership:

Prefer

```rust
fn from_utf8_owned(vv: Vec<u8>) -> Result<String, Vec<u8>>
```

over

```rust
fn from_utf8_owned(vv: Vec<u8>) -> Option<String>
```

The `from_utf8_owned` function gains ownership of a vector.  In the successful
case, the function consumes its input, returning an owned string without
allocating or copying. In the unsuccessful case, however, the function returns
back ownership of the original slice.