Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

book: Change 'unified function call syntax' to 'associated item loookup syntax' #25965

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/doc/trpl/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* [if let](if-let.md)
* [Trait Objects](trait-objects.md)
* [Closures](closures.md)
* [Universal Function Call Syntax](ufcs.md)
* [Associated Item Lookup Syntax](ails.md)
* [Crates and Modules](crates-and-modules.md)
* [`const` and `static`](const-and-static.md)
* [Attributes](attributes.md)
Expand Down
66 changes: 61 additions & 5 deletions src/doc/trpl/ufcs.md → src/doc/trpl/ails.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
% Universal Function Call Syntax
% Associated Item Lookup Syntax

Sometimes, functions can have the same names. Consider this code:
Sometimes, the functions associated with traits can have the same names.
Consider this code:

```rust
trait Foo {
Expand Down Expand Up @@ -41,8 +42,8 @@ note: candidate #2 is defined in an impl of the trait `main::Bar` for the type

```

We need a way to disambiguate which method we need. This feature is called
‘universal function call syntax’, and it looks like this:
We need a way to disambiguate which method we need. There is a syntax we can
use, and it looks like this:

```rust
# trait Foo {
Expand Down Expand Up @@ -86,7 +87,7 @@ not, and so we need to pass an explicit `&b`.

# Angle-bracket Form

The form of UFCS we just talked about:
The form of function call syntax we just talked about:

```rust,ignore
Trait::method(args);
Expand Down Expand Up @@ -125,3 +126,58 @@ impl Foo for Bar {
```

This will call the `Clone` trait’s `clone()` method, rather than `Foo`’s.

# Associated Item Lookup Syntax

In addition to being used for function calls, the syntax discussed above can
also be used to disambiguate lookups of associated types and constants. Consider
the following code:

```rust
trait Foo {
type Quux;
}

trait Bar {
type Quux;
}

struct Baz;

impl Foo for Baz {
type Quux = i32;
}

impl Bar for Baz {
type Quux = u32;
}
```

Trying to use the type `Baz::Quux` would be ambiguous, since there are two
possible types that match. To disambiguate, the `<Type as Trait>` syntax can be
used:

```rust
# trait Foo {
# type Quux;
# }

# trait Bar {
# type Quux;
# }
#
# struct Baz;
#
# impl Foo for Baz {
# type Quux = i32;
# }
#
# impl Bar for Baz {
# type Quux = u32;
# }
let x: <Baz as Foo>::Quux = 5;
```

This syntax is sometimes called 'associated item lookup syntax', but you can
just think of it as a way to disambiguate lookup of associated functions,
types, and constants.