Skip to content

Commit 1688e48

Browse files
committed
minor
1 parent 6976494 commit 1688e48

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

docs/dev/style.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ Avoid preconditions that span across function boundaries:
113113

114114
```rust
115115
// Good
116+
fn main() {
117+
let s: &str = ...;
118+
if let Some(contents) = string_literal_contents(s) {
119+
120+
}
121+
}
122+
116123
fn string_literal_contents(s: &str) -> Option<&str> {
117124
if s.starts_with('"') && s.ends_with('"') {
118125
Some(&s[1..s.len() - 1])
@@ -121,24 +128,17 @@ fn string_literal_contents(s: &str) -> Option<&str> {
121128
}
122129
}
123130

124-
fn foo() {
131+
// Not as good
132+
fn main() {
125133
let s: &str = ...;
126-
if let Some(contents) = string_literal_contents(s) {
127-
134+
if is_string_literal(s) {
135+
let contents = &s[1..s.len() - 1];
128136
}
129137
}
130138

131-
// Not as good
132139
fn is_string_literal(s: &str) -> bool {
133140
s.starts_with('"') && s.ends_with('"')
134141
}
135-
136-
fn foo() {
137-
let s: &str = ...;
138-
if is_string_literal(s) {
139-
let contents = &s[1..s.len() - 1];
140-
}
141-
}
142142
```
143143

144144
In the "Not as good" version, the precondition that `1` is a valid char boundary is checked in `is_string_literal` and used in `foo`.

0 commit comments

Comments
 (0)