diff options
-rw-r--r-- | docs/dev/style.md | 22 |
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 |
116 | fn main() { | ||
117 | let s: &str = ...; | ||
118 | if let Some(contents) = string_literal_contents(s) { | ||
119 | |||
120 | } | ||
121 | } | ||
122 | |||
116 | fn string_literal_contents(s: &str) -> Option<&str> { | 123 | fn 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 | ||
124 | fn foo() { | 131 | // Not as good |
132 | fn 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 | ||
132 | fn is_string_literal(s: &str) -> bool { | 139 | fn is_string_literal(s: &str) -> bool { |
133 | s.starts_with('"') && s.ends_with('"') | 140 | s.starts_with('"') && s.ends_with('"') |
134 | } | 141 | } |
135 | |||
136 | fn 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 | ||
144 | 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`. | 144 | 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`. |