Skip to content

Commit 7090953

Browse files
committed
manual: rewrite the section on type kinds.
1 parent 9bbff50 commit 7090953

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

doc/rust.md

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,40 +2748,45 @@ call to the method `to_str`.
27482748

27492749
## Type kinds
27502750

2751-
Types in Rust are categorized into three kinds, based on whether they
2752-
allow copying of their values, and sending to different tasks. The
2753-
kinds are:
2754-
2755-
Sendable
2756-
: Values with a sendable type can be safely sent to another task.
2751+
Types in Rust are categorized into kinds, based on various properties of the components of the type.
2752+
The kinds are:
2753+
2754+
`Const`
2755+
: Types of this kind are deeply immutable;
2756+
they contain no mutable memory locations directly or indirectly via pointers.
2757+
`Send`
2758+
: Types of this kind can be safely sent between tasks.
27572759
This kind includes scalars, owning pointers, owned closures, and
27582760
structural types containing only other sendable types.
2759-
Copyable
2761+
`Owned`
2762+
: Types of this kind do not contain any borrowed pointers;
2763+
this can be a useful guarantee for code that breaks borrowing assumptions using [`unsafe` operations](#unsafe-functions).
2764+
`Copy`
27602765
: This kind includes all types that can be copied. All types with
27612766
sendable kind are copyable, as are managed boxes, managed closures,
27622767
trait types, and structural types built out of these.
2763-
Noncopyable
2764-
: [Resource](#resources) types, and every type that includes a
2765-
resource without storing it in a managed box, may not be copied.
2766-
Types of sendable or copyable type can always be used in places
2767-
where a noncopyable type is expected, so in effect this kind
2768-
includes all types.
2769-
2770-
These form a hierarchy. The noncopyable kind is the widest, including
2771-
all types in the language. The copyable kind is a subset of that, and
2772-
the sendable kind is a subset of the copyable kind.
2773-
2774-
Any operation that causes a value to be copied requires the type of
2775-
that value to be of copyable kind. Type parameter types are assumed to
2776-
be noncopyable, unless one of the special bounds `send` or `copy` is
2777-
declared for it. For example, this is not a valid program:
2768+
_Default_
2769+
: Types with destructors, closure environments,
2770+
and various other _non-first-class_ types,
2771+
are not copyable at all.
2772+
Such types can usually only be accessed through pointers,
2773+
or in some cases, moved between mutable locations.
2774+
2775+
Kinds can be supplied as _bounds_ on type parameters, like traits,
2776+
in which case the parameter is constrained to types satisfying that kind.
2777+
2778+
By default, type parameters do not carry any assumed kind-bounds at all.
2779+
2780+
Any operation that causes a value to be copied requires the type of that value to be of copyable kind,
2781+
so the `Copy` bound is frequently required on function type parameters.
2782+
For example, this is not a valid program:
27782783

27792784
~~~~{.xfail-test}
27802785
fn box<T>(x: T) -> @T { @x }
27812786
~~~~
27822787

2783-
Putting `x` into a managed box involves copying, and the `T` parameter
2784-
is assumed to be noncopyable. To change that, a bound is declared:
2788+
Putting `x` into a managed box involves copying, and the `T` parameter has the default (non-copyable) kind.
2789+
To change that, a bound is declared:
27852790

27862791
~~~~
27872792
fn box<T: Copy>(x: T) -> @T { @x }

0 commit comments

Comments
 (0)