aboutsummaryrefslogtreecommitdiff
path: root/docs/dev/style.md
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-10-07 12:03:13 +0100
committerAleksey Kladov <[email protected]>2020-10-07 12:03:13 +0100
commit1688e481b31e0f67fba72beee4079adb6b95f83c (patch)
tree29c6fdbc4e8a1c313886c8690585d21799ed43c2 /docs/dev/style.md
parent6976494781f810193535eebc73e5bda73ba9eddf (diff)
minor
Diffstat (limited to 'docs/dev/style.md')
-rw-r--r--docs/dev/style.md22
1 files changed, 11 insertions, 11 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md
index cc06d4122..7aed7816e 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -113,6 +113,13 @@ Avoid preconditions that span across function boundaries:
113 113
114```rust 114```rust
115// Good 115// Good
116fn main() {
117 let s: &str = ...;
118 if let Some(contents) = string_literal_contents(s) {
119
120 }
121}
122
116fn string_literal_contents(s: &str) -> Option<&str> { 123fn string_literal_contents(s: &str) -> Option<&str> {
117 if s.starts_with('"') && s.ends_with('"') { 124 if s.starts_with('"') && s.ends_with('"') {
118 Some(&s[1..s.len() - 1]) 125 Some(&s[1..s.len() - 1])
@@ -121,24 +128,17 @@ fn string_literal_contents(s: &str) -> Option<&str> {
121 } 128 }
122} 129}
123 130
124fn foo() { 131// Not as good
132fn main() {
125 let s: &str = ...; 133 let s: &str = ...;
126 if let Some(contents) = string_literal_contents(s) { 134 if is_string_literal(s) {
127 135 let contents = &s[1..s.len() - 1];
128 } 136 }
129} 137}
130 138
131// Not as good
132fn is_string_literal(s: &str) -> bool { 139fn is_string_literal(s: &str) -> bool {
133 s.starts_with('"') && s.ends_with('"') 140 s.starts_with('"') && s.ends_with('"')
134} 141}
135
136fn foo() {
137 let s: &str = ...;
138 if is_string_literal(s) {
139 let contents = &s[1..s.len() - 1];
140 }
141}
142``` 142```
143 143
144In the "Not as good" version, the precondition that `1` is a valid char boundary is checked in `is_string_literal` and used in `foo`. 144In the "Not as good" version, the precondition that `1` is a valid char boundary is checked in `is_string_literal` and used in `foo`.