diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2020-10-07 13:03:13 +0200 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2020-10-07 13:03:13 +0200 |
| commit | 1688e481b31e0f67fba72beee4079adb6b95f83c (patch) | |
| tree | 29c6fdbc4e8a1c313886c8690585d21799ed43c2 /docs/dev | |
| parent | 6976494781f810193535eebc73e5bda73ba9eddf (diff) | |
| download | rust-1688e481b31e0f67fba72beee4079adb6b95f83c.tar.gz rust-1688e481b31e0f67fba72beee4079adb6b95f83c.zip | |
minor
Diffstat (limited to 'docs/dev')
| -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 cc06d41221e..7aed7816ec9 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -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`. |
