Skip to content

Commit 3053f49

Browse files
committed
Discuss unambiguous function call syntax
And shrink the undocumented list
1 parent 6b0de90 commit 3053f49

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/expressions.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ exact `self`-type of the left-hand-side is known, or dynamically dispatching if
220220
the left-hand-side expression is an indirect [trait
221221
object](types.html#trait-objects).
222222

223+
[UFCS](#unambiguous-function-call-syntax) allows method invocation using
224+
function-like syntax.
225+
223226
## Field expressions
224227

225228
A _field expression_ consists of an expression followed by a single dot and an
@@ -544,6 +547,41 @@ let x: i32 = add(1i32, 2i32);
544547
let pi: Result<f32, _> = "3.14".parse();
545548
```
546549

550+
Function calls may sometimes result in ambiguities about receiver or referent.
551+
See [UFCS](#unambiguous-function-call-syntax) for how to resolve this.
552+
553+
### Unambiguous Function Call Syntax
554+
555+
Several situations often occur which result in ambiguities about the receiver or
556+
referent of method or associated function calls. These situations may include:
557+
558+
* Multiple in-scope traits define the same method for the same types
559+
* Auto-`deref` is undesirable; for example, distinguishing between methods on a
560+
smart pointer itself and the pointer's referent.
561+
* Methods which take no arguments and return properties of a type, like
562+
`SizeOf::size_of()`
563+
564+
The programmer may unambiguously refer to their desired method or function using
565+
Unambiguous Function Call Syntax (UFCS), specifying only as much type and path
566+
information as necessary.
567+
568+
For example,
569+
570+
```rust,ignore
571+
// shorthand, only if `T` is a path
572+
T::size_of()
573+
574+
// infer trait `SizeOf` based on traits in scope.
575+
<T>::size_of()
576+
577+
// completely unambiguous; works if multiple in-scope traits define `size_of()`
578+
<T as SizeOf>::size_of()
579+
```
580+
581+
Refer to [RFC 132] for further details, motivations, and subtleties of syntax.
582+
583+
[RFC 132]: https://github.com/rust-lang/rfcs/blob/master/text/0132-ufcs.md
584+
547585
## Lambda expressions
548586

549587
A _lambda expression_ (sometimes called an "anonymous function expression")

src/undocumented.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ to shrink!
1414
specified.
1515
- [Flexible target specification] - Some---but not all---flags are documented
1616
in [Conditional compilation]
17-
- [Unambiguous function call syntax]
1817
- [Require parentheses for chained comparisons]
1918
- [Integer overflow not `unsafe`] - documented with a reference to the RFC, but
2019
requires further details

0 commit comments

Comments
 (0)