File tree Expand file tree Collapse file tree 1 file changed +11
-11
lines changed Expand file tree Collapse file tree 1 file changed +11
-11
lines changed Original file line number Diff line number Diff 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+
116123fn 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
132139fn 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
144144In the "Not as good" version, the precondition that ` 1 ` is a valid char boundary is checked in ` is_string_literal ` and used in ` foo ` .
You can’t perform that action at this time.
0 commit comments