Skip to content

Commit 45d1cd8

Browse files
committed
Copyedit "Items and attributes" section in docs
Most notably, I removed the "foldl" example in the section on pure functions, as IIRC this is no longer something you need an unsafe block for (pure functions are as pure as their arguments). Feel free to add an example where an unsafe block really is needed.
1 parent 39c0d35 commit 45d1cd8

File tree

1 file changed

+44
-81
lines changed

1 file changed

+44
-81
lines changed

doc/rust.md

Lines changed: 44 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -703,22 +703,19 @@ otherwise compose the item body. The meaning of these scoped items is the same
703703
as if the item was declared outside the scope -- it is still a static item --
704704
except that the item's *path name* within the module namespace is qualified by
705705
the name of the enclosing item, or is private to the enclosing item (in the
706-
case of functions). The exact locations in which sub-items may be declared is
707-
given by the grammar.
706+
case of functions).
707+
The grammar specifies the exact locations in which sub-item declarations may appear.
708708

709709
### Type Parameters
710710

711-
All items except modules may be *parametrized* by type. Type parameters are
711+
All items except modules may be *parameterized* by type. Type parameters are
712712
given as a comma-separated list of identifiers enclosed in angle brackets
713-
(`<...>`), after the name of the item and before its definition. The type
714-
parameters of an item are considered "part of the name", not the type of the
715-
item; in order to refer to the type-parametrized item, a referencing
716-
[path](#paths) must in general provide type arguments as a list of
717-
comma-separated types enclosed within angle brackets. In practice, the
718-
type-inference system can usually infer such argument types from
719-
context. There are no general type-parametric types, only type-parametric
720-
items.
721-
713+
(`<...>`), after the name of the item and before its definition.
714+
The type parameters of an item are considered "part of the name", not part of the type of the item.
715+
A referencing [path](#paths) must (in principle) provide type arguments as a list of comma-separated types enclosed within angle brackets, in order to refer to the type-parameterized item.
716+
In practice, the type-inference system can usually infer such argument types from context.
717+
There are no general type-parametric types, only type-parametric items.
718+
That is, Rust has no notion of type abstraction: there are no first-class "forall" types.
722719

723720
### Modules
724721

@@ -763,9 +760,9 @@ mod math {
763760
view_item : extern_mod_decl | use_decl ;
764761
~~~~~~~~
765762

766-
A view item manages the namespace of a module; it does not define new items
767-
but simply changes the visibility of other items. There are several kinds of
768-
view item:
763+
A view item manages the namespace of a module.
764+
View items do not define new items, but rather, simply change other items' visibilit.
765+
There are several kinds of view item:
769766

770767
* [`extern mod` declarations](#extern-mod-declarations)
771768
* [`use` declarations](#use-declarations)
@@ -789,7 +786,7 @@ compiler's library path and matching the `link_attrs` provided in the
789786
crate when it was compiled. If no `link_attrs` are provided, a default `name`
790787
attribute is assumed, equal to the `ident` given in the `use_decl`.
791788

792-
Two examples of `extern mod` declarations:
789+
Three examples of `extern mod` declarations:
793790

794791
~~~~~~~~{.xfail-test}
795792
extern mod pcre (uuid = "54aba0f8-a7b1-4beb-92f1-4cf625264841");
@@ -844,8 +841,8 @@ fn main() {
844841
log(info, Some(1.0));
845842
846843
// Equivalent to 'log(core::info,
847-
// core::str::to_upper(core::str::slice("foo", 0u, 1u)));'
848-
log(info, to_upper(slice("foo", 0u, 1u)));
844+
// core::str::to_upper(core::str::slice("foo", 0, 1)));'
845+
log(info, to_upper(slice("foo", 0, 1)));
849846
}
850847
~~~~
851848

@@ -857,13 +854,9 @@ If a sequence of such redirections form a cycle or cannot be unambiguously resol
857854

858855
### Functions
859856

860-
A _function item_ defines a sequence of [statements](#statements) and an
861-
optional final [expression](#expressions) associated with a name and a set of
862-
parameters. Functions are declared with the keyword `fn`. Functions declare a
863-
set of *input* [*slots*](#memory-slots) as parameters, through which the
864-
caller passes arguments into the function, and an *output*
865-
[*slot*](#memory-slots) through which the function passes results back to
866-
the caller.
857+
A _function item_ defines a sequence of [statements](#statements) and an optional final [expression](#expressions), along with a name and a set of parameters.
858+
Functions are declared with the keyword `fn`.
859+
Functions declare a set of *input* [*slots*](#memory-slots) as parameters, through which the caller passes arguments into the function, and an *output* [*slot*](#memory-slots) through which the function passes results back to the caller.
867860

868861
A function may also be copied into a first class *value*, in which case the
869862
value has the corresponding [*function type*](#function-types), and can be
@@ -941,23 +934,19 @@ Specifically, the following operations are considered unsafe:
941934

942935
##### Unsafe blocks
943936

944-
A block of code can also be prefixed with the `unsafe` keyword,
945-
to permit a sequence of unsafe operations in an otherwise-safe function.
946-
This facility exists because the static semantics of a Rust are a necessary approximation of the dynamic semantics.
947-
When a programmer has sufficient conviction that a sequence of unsafe operations is actually safe,
948-
they can encapsulate that sequence (taken as a whole) within an `unsafe` block.
949-
The compiler will consider uses of such code "safe", to the surrounding context.
937+
A block of code can also be prefixed with the `unsafe` keyword, to permit a sequence of unsafe operations in an otherwise-safe function.
938+
This facility exists because the static semantics of Rust are a necessary approximation of the dynamic semantics.
939+
When a programmer has sufficient conviction that a sequence of unsafe operations is actually safe, they can encapsulate that sequence (taken as a whole) within an `unsafe` block. The compiler will consider uses of such code "safe", to the surrounding context.
950940

951941

952942
#### Pure functions
953943

954944
A pure function declaration is identical to a function declaration, except that
955945
it is declared with the additional keyword `pure`. In addition, the typechecker
956946
checks the body of a pure function with a restricted set of typechecking rules.
957-
A pure function
958-
959-
* may not contain an assignment or self-call expression; and
960-
* may only call other pure functions, not general functions.
947+
A pure function may only modify data owned by its own stack frame.
948+
So, a pure function may modify a local variable allocated on the stack, but not a mutable reference that it takes as an argument.
949+
A pure function may only call other pure functions, not general functions.
961950

962951
An example of a pure function:
963952

@@ -978,36 +967,13 @@ pure fn nonempty_list<T>(ls: List<T>) -> bool { pure_length(ls) > 0u }
978967
These purity-checking rules approximate the concept of referential transparency:
979968
that a call-expression could be rewritten with the literal-expression of its return value, without changing the meaning of the program.
980969
Since they are an approximation, sometimes these rules are *too* restrictive.
981-
Rust allows programmers to violate these rules using [`unsafe` blocks](#unsafe-blocks).
970+
Rust allows programmers to violate these rules using [`unsafe` blocks](#unsafe-blocks), which we already saw.
982971
As with any `unsafe` block, those that violate static purity carry transfer the burden of safety-proof from the compiler to the programmer.
983972
Programmers should exercise caution when breaking such rules.
984973

985-
An example of a pure function that uses an unsafe block:
986-
987-
~~~~ {.xfail-test}
988-
# use std::list::*;
989-
990-
fn pure_foldl<T, U: Copy>(ls: List<T>, u: U, f: fn(&T, &U) -> U) -> U {
991-
match ls {
992-
Nil => u,
993-
Cons(hd, tl) => f(hd, pure_foldl(*tl, f(hd, u), f))
994-
}
995-
}
996-
997-
pure fn pure_length<T>(ls: List<T>) -> uint {
998-
fn count<T>(_t: &T, u: &uint) -> uint { *u + 1u }
999-
unsafe {
1000-
pure_foldl(ls, 0u, count)
1001-
}
1002-
}
1003-
~~~~
1004-
1005-
Despite its name, `pure_foldl` is a `fn`, not a `pure fn`, because there is no
1006-
way in Rust to specify that the higher-order function argument `f` is a pure
1007-
function. So, to use `foldl` in a pure list length function that a pure function
1008-
could then use, we must use an `unsafe` block wrapped around the call to
1009-
`pure_foldl` in the definition of `pure_length`.
974+
For more details on purity, see [the borrowed pointer tutorial][borrow].
1010975

976+
[borrow]: tutorial-borrowed-ptr.html
1011977

1012978
#### Diverging functions
1013979

@@ -1092,10 +1058,8 @@ specific type; the type-specified aspects of a value include:
10921058
* The sequence of memory operations required to access the value.
10931059
* The [kind](#type-kinds) of the type.
10941060

1095-
For example, the type `{x: u8, y: u8`} defines the set of immutable values
1096-
that are composite records, each containing two unsigned 8-bit integers
1097-
accessed through the components `x` and `y`, and laid out in memory with the
1098-
`x` component preceding the `y` component.
1061+
For example, the type `(u8, u8)` defines the set of immutable values that are composite pairs,
1062+
each containing two unsigned 8-bit integers accessed by pattern-matching and laid out in memory with the `x` component preceding the `y` component.
10991063

11001064
### Structures
11011065

@@ -1111,7 +1075,7 @@ let px: int = p.x;
11111075

11121076
### Enumerations
11131077

1114-
An _enumeration_ is a simulatneous definition of a nominal [enumerated type](#enumerated-types) as well as a set of *constructors*,
1078+
An _enumeration_ is a simultaneous definition of a nominal [enumerated type](#enumerated-types) as well as a set of *constructors*,
11151079
that can be used to create or pattern-match values of the corresponding enumerated type.
11161080

11171081
Enumerations are declared with the keyword `enum`.
@@ -1134,7 +1098,7 @@ a = Cat;
11341098
const_item : "const" ident ':' type '=' expr ';' ;
11351099
~~~~~~~~
11361100

1137-
A Constant is a named value stored in read-only memory in a crate.
1101+
A *constant* is a named value stored in read-only memory in a crate.
11381102
The value bound to a constant is evaluated at compile time.
11391103
Constants are declared with the `const` keyword.
11401104
A constant item must have an expression giving its definition.
@@ -1206,7 +1170,7 @@ let myshape: Shape = @mycircle as @Shape;
12061170
~~~~
12071171

12081172
The resulting value is a managed box containing the value that was cast,
1209-
along with information that identify the methods of the implementation that was used.
1173+
along with information that identifies the methods of the implementation that was used.
12101174
Values with a trait type can have [methods called](#method-call-expressions) on them,
12111175
for any method in the trait,
12121176
and can be used to instantiate type parameters that are bounded by the trait.
@@ -1271,9 +1235,9 @@ foreign_mod : [ foreign_fn ] * ;
12711235

12721236
Foreign modules form the basis for Rust's foreign function interface. A
12731237
foreign module describes functions in external, non-Rust
1274-
libraries. Functions within foreign modules are declared the same as other
1275-
Rust functions, with the exception that they may not have a body and are
1276-
instead terminated by a semi-colon.
1238+
libraries.
1239+
Functions within foreign modules are declared in the same way as other Rust functions,
1240+
with the exception that they may not have a body and are instead terminated by a semicolon.
12771241

12781242
~~~
12791243
# use libc::{c_char, FILE};
@@ -1284,9 +1248,8 @@ extern mod c {
12841248
}
12851249
~~~
12861250

1287-
Functions within foreign modules may be called by Rust code as it would any
1288-
normal function and the Rust compiler will automatically translate between
1289-
the Rust ABI and the foreign ABI.
1251+
Functions within foreign modules may be called by Rust code, just like functions defined in Rust.
1252+
The Rust compiler automatically translates between the Rust ABI and the foreign ABI.
12901253

12911254
The name of the foreign module has special meaning to the Rust compiler in
12921255
that it will treat the module name as the name of a library to link to,
@@ -1300,7 +1263,7 @@ A number of [attributes](#attributes) control the behavior of foreign
13001263
modules.
13011264

13021265
By default foreign modules assume that the library they are calling use the
1303-
standard C "cdecl" ABI. Other ABI's may be specified using the `abi`
1266+
standard C "cdecl" ABI. Other ABIs may be specified using the `abi`
13041267
attribute as in
13051268

13061269
~~~{.xfail-test}
@@ -1310,15 +1273,15 @@ extern mod kernel32 { }
13101273
~~~
13111274

13121275
The `link_name` attribute allows the default library naming behavior to
1313-
be overriden by explicitly specifying the name of the library.
1276+
be overridden by explicitly specifying the name of the library.
13141277

13151278
~~~{.xfail-test}
13161279
#[link_name = "crypto"]
13171280
extern mod mycrypto { }
13181281
~~~
13191282

1320-
The `nolink` attribute tells the Rust compiler not to perform any linking
1321-
for the foreign module. This is particularly useful for creating foreign
1283+
The `nolink` attribute tells the Rust compiler not to do any linking for the foreign module.
1284+
This is particularly useful for creating foreign
13221285
modules for libc, which tends to not follow standard library naming
13231286
conventions and is linked to all Rust programs anyway.
13241287

@@ -1333,9 +1296,9 @@ attr : ident [ '=' literal
13331296

13341297
Static entities in Rust -- crates, modules and items -- may have _attributes_
13351298
applied to them. ^[Attributes in Rust are modeled on Attributes in ECMA-335,
1336-
C#] An attribute is a general, free-form piece of metadata that is interpreted
1337-
according to name, convention, and language and compiler version. Attributes
1338-
may appear as any of:
1299+
C#]
1300+
An attribute is a general, free-form metadatum that is interpreted according to name, convention, and language and compiler version.
1301+
Attributes may appear as any of
13391302

13401303
* A single identifier, the attribute name
13411304
* An identifier followed by the equals sign '=' and a literal, providing a key/value pair

0 commit comments

Comments
 (0)