]> git.proxmox.com Git - rustc.git/blame - src/doc/style/features/functions-and-methods/output.md
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / doc / style / features / functions-and-methods / output.md
CommitLineData
85aaf69f
SL
1% Output from functions and methods
2
3### Don't overpromise. [FIXME]
4
5> **[FIXME]** Add discussion of overly-specific return types,
6> e.g. returning a compound iterator type rather than hiding it behind
7> a use of newtype.
8
9### Let clients choose what to throw away. [FIXME: needs RFC]
10
11#### Return useful intermediate results:
12
13Many functions that answer a question also compute interesting related data. If
14this data is potentially of interest to the client, consider exposing it in the
15API.
16
17Prefer
18
19```rust
20struct SearchResult {
21 found: bool, // item in container?
62682a34 22 expected_index: usize // what would the item's index be?
85aaf69f
SL
23}
24
25fn binary_search(&self, k: Key) -> SearchResult
26```
27or
28
29```rust
62682a34 30fn binary_search(&self, k: Key) -> (bool, usize)
85aaf69f
SL
31```
32
33over
34
35```rust
36fn binary_search(&self, k: Key) -> bool
37```
38
39#### Yield back ownership:
40
41Prefer
42
43```rust
44fn from_utf8_owned(vv: Vec<u8>) -> Result<String, Vec<u8>>
45```
46
47over
48
49```rust
50fn from_utf8_owned(vv: Vec<u8>) -> Option<String>
51```
52
53The `from_utf8_owned` function gains ownership of a vector. In the successful
54case, the function consumes its input, returning an owned string without
55allocating or copying. In the unsuccessful case, however, the function returns
56back ownership of the original slice.