From 1688e481b31e0f67fba72beee4079adb6b95f83c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 7 Oct 2020 13:03:13 +0200 Subject: minor --- docs/dev/style.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'docs/dev') 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: ```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`. -- cgit v1.2.3