about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJosh Triplett <josh@joshtriplett.org>2023-07-31 16:07:09 -0700
committerJosh Triplett <josh@joshtriplett.org>2023-08-04 01:23:49 -0700
commit42fa7df38cea93d1a1991b7aa085a7a57b2800fa (patch)
tree7b148d790667f91914f05ccd85b7fa40ab08f1a3
parent736ef39a84cafc879818781094c897bfbb0bf7bc (diff)
downloadrust-42fa7df38cea93d1a1991b7aa085a7a57b2800fa.tar.gz
rust-42fa7df38cea93d1a1991b7aa085a7a57b2800fa.zip
style-guide: Expand documentation on `as` casts (mostly like a binary operator)
`as` casts currently get formatted like a binary operator, except that
the second line can stack several `as` casts rather than breaking them
each onto their own line. Merge `as` into a subsection of the binary
operators section, and then go into detail on the one difference between
`as` formatting and binary operator formatting.
-rw-r--r--src/doc/style-guide/src/expressions.md28
1 files changed, 20 insertions, 8 deletions
diff --git a/src/doc/style-guide/src/expressions.md b/src/doc/style-guide/src/expressions.md
index 32c604f9f3e..91dd3d2224a 100644
--- a/src/doc/style-guide/src/expressions.md
+++ b/src/doc/style-guide/src/expressions.md
@@ -328,6 +328,26 @@ foo_bar
 Prefer line-breaking at an assignment operator (either `=` or `+=`, etc.) rather
 than at other binary operators.
 
+### Casts (`as`)
+
+Format `as` casts like a binary operator. In particular, always include spaces
+around `as`, and if line-breaking, break before the `as` (never after) and
+block-indent the subsequent line. Format the type on the right-hand side using
+the rules for types.
+
+However, unlike with other binary operators, if chaining a series of `as` casts
+that require line-breaking, and line-breaking before the first `as` suffices to
+make the remainder fit on the next line, don't break before any subsequent
+`as`; instead, leave the series of types all on the same line:
+
+```rust
+let cstr = very_long_expression()
+    as *const str as *const [u8] as *const std::os::raw::c_char;
+```
+
+If the subsequent line still requires line-breaking, break and block-indent
+before each `as` as with other binary operators.
+
 ## Control flow
 
 Do not include extraneous parentheses for `if` and `while` expressions.
@@ -421,14 +441,6 @@ assert_eq!(
 );
 ```
 
-## Casts (`as`)
-
-Put spaces before and after `as`:
-
-```rust
-let cstr = "Hi\0" as *const str as *const [u8] as *const std::os::raw::c_char;
-```
-
 ## Chains of fields and method calls
 
 A chain is a sequence of field accesses, method calls, and/or uses of the try