Extend rust reference with a section about type coercions · rust-lang/reference@4c39f37 (original) (raw)

`@@ -3619,6 +3619,126 @@ fn bar<'a>() {

`

3619

3619

`` Since 'static "lives longer" than 'a, &'static str is a subtype of

``

3620

3620

`` &'a str.

``

3621

3621

``

``

3622

`+

Type coercions

`

``

3623

+

``

3624

`+

Coercions are defined in RFC401. A coercion is implicit and has no syntax.

`

``

3625

+

``

3626

`+

`

``

3627

+

``

3628

`+

Coercion sites

`

``

3629

+

``

3630

`+

A coercion can only occur at certain coercion sites in a program; these are

`

``

3631

`+

typically places where the desired type is explicit or can be dervied by

`

``

3632

`+

propagation from explicit types (without type inference). Possible coercion

`

``

3633

`+

sites are:

`

``

3634

+

``

3635

`` +

``

``

3636

+

``

3637

`` +

In let _: U = e;, e is coerced to have type U.

``

``

3638

+

``

3639

`` +

``

``

3640

+

``

3641

`+

`

``

3642

+

``

3643

`+

The value being coerced is the

`

``

3644

`+

actual parameter and it is coerced to the type of the formal parameter. For

`

``

3645

`` +

example, let foo be defined as fn foo(x: U) { ... } and call it as

``

``

3646

`` +

foo(e);. Then e is coerced to have type U;

``

``

3647

+

``

3648

`+

`

``

3649

+

``

3650

`` +

Assume we have a `struct

``

``

3651

`` +

Foo { x: U }and instantiate it asFoo { x: e }. Then e` is coerced to

``

``

3652

`` +

have type U.

``

``

3653

+

``

3654

`+

`

``

3655

`` +

terminated or any expression in a return statement).

``

``

3656

+

``

3657

`` +

In fn foo() -> U { e }, e is coerced to to have type U.

``

``

3658

+

``

3659

`+

If the expression in one of these coercion sites is a coercion-propagating

`

``

3660

`+

expression, then the relevant sub-expressions in that expression are also

`

``

3661

`+

coercion sites. Propagation recurses from these new coercion sites.

`

``

3662

`+

Propagating expressions and their relevant sub-expressions are:

`

``

3663

+

``

3664

`` +

``

``

3665

`` +

the array literal is a coercion site for coercion to type U.

``

``

3666

+

``

3667

`` +

``

``

3668

`` +

repeated sub-expression is a coercion site for coercion to type U.

``

``

3669

+

``

3670

`` +

``

``

3671

`+

Each sub-expression is a coercion site to the respective type, e.g. the

`

``

3672

`` +

zeroth sub-expression is a coercion site to type U_0.

``

``

3673

+

``

3674

`` +

``

``

3675

`` +

the sub-expression is a coercion site to U.

``

``

3676

+

``

3677

`` +

``

``

3678

`` +

it is not semicolon-terminated) is a coercion site to U. This includes

``

``

3679

`` +

blocks which are part of control flow statements, such as if/else, if

``

``

3680

`+

the block has a known type.

`

``

3681

+

``

3682

`+

Coercion types

`

``

3683

+

``

3684

`+

Coercion is allowed between the following types:

`

``

3685

+

``

3686

`` +

``

``

3687

+

``

3688

`` +

``

``

3689

`+

(transitive case).

`

``

3690

+

``

3691

`+

Note that this is not fully supported yet

`

``

3692

+

``

3693

`` +

``

``

3694

+

``

3695

`` +

``

``

3696

+

``

3697

`` +

``

``

3698

+

``

3699

`` +

``

``

3700

+

``

3701

`` +

``

``

3702


```

``

3703

`+

use std::ops::Deref;

`

``

3704

+

``

3705

`+

struct CharContainer {

`

``

3706

`+

value: char

`

``

3707

`+

}

`

``

3708

+

``

3709

`+

impl Deref for CharContainer {

`

``

3710

`+

type Target = char;

`

``

3711

+

``

3712

`+

fn deref<'a>(&'a self) -> &'a char {

`

``

3713

`+

&self.value

`

``

3714

`+

}

`

``

3715

`+

}

`

``

3716

+

``

3717

`+

fn foo(arg: &char) {}

`

``

3718

+

``

3719

`+

fn main() {

`

``

3720

`+

let x = &mut CharContainer { value: 'y' };

`

``

3721

`+

foo(x); //&mut CharContainer is coerced to &char.

`

``

3722

`+

}

`

``

3723


 ```

``

3724

`` +

``

``

3725

+

``

3726

`` +

``

``

3727

`` +

``

``

3728

`` +

``

``

3729

`` +

``

``

3730

`` +

``

``

3731

`` +

``

``

3732

+

``

3733

`+

and where

`

``

3734

`` +

``

``

3735

`` +

``

``

3736

`` +

trait U.

``

``

3737

+

``

3738

`+

In the future, coerce_inner will be recursively extended to tuples and

`

``

3739

`+

structs. In addition, coercions from sub-traits to super-traits will be

`

``

3740

`+

added. See RFC401 for more details.

`

``

3741

+

3622

3742

`# Special traits

`

3623

3743

``

3624

3744

`Several traits define special evaluation behavior.

`