]> git.lizzy.rs Git - rust.git/commitdiff
minor
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 7 Oct 2020 11:03:13 +0000 (13:03 +0200)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 7 Oct 2020 11:03:13 +0000 (13:03 +0200)
docs/dev/style.md

index cc06d41221e8d9c0c0236c1db4792d01a35ffd0a..7aed7816ec9a004e2fa3e62ce50d7a04c97e82cf 100644 (file)
@@ -113,6 +113,13 @@ Avoid preconditions that span across function boundaries:
 
 ```rust
 // Good
+fn main() {
+    let s: &str = ...;
+    if let Some(contents) = string_literal_contents(s) {
+
+    }
+}
+
 fn string_literal_contents(s: &str) -> Option<&str> {
     if s.starts_with('"') && s.ends_with('"') {
         Some(&s[1..s.len() - 1])
@@ -121,24 +128,17 @@ fn string_literal_contents(s: &str) -> Option<&str> {
     }
 }
 
-fn foo() {
+// Not as good
+fn main() {
     let s: &str = ...;
-    if let Some(contents) = string_literal_contents(s) {
-
+    if is_string_literal(s) {
+        let contents = &s[1..s.len() - 1];
     }
 }
 
-// Not as good
 fn is_string_literal(s: &str) -> bool {
     s.starts_with('"') && s.ends_with('"')
 }
-
-fn foo() {
-    let s: &str = ...;
-    if is_string_literal(s) {
-        let contents = &s[1..s.len() - 1];
-    }
-}
 ```
 
 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`.