Skip to content

Commit d58a9c7

Browse files
committed
Expand the tutorial section on functions
1 parent 769e9b6 commit d58a9c7

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

doc/tutorial/func.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ not see changes made to these variables after the `lambda` was
4040
evaluated. `lambda`s can be put in data structures and passed around
4141
without limitation.
4242

43+
The type of a closure is `lambda(args) -> type`, as opposed to
44+
`fn(args) -> type`. The `fn` type stands for 'bare' functions, with no
45+
closure attached. Keep this in mind when writing higher-order
46+
functions.
47+
4348
A different form of closure is the block. Blocks are written like they
4449
are in Ruby: `{|x| x + y}`, the formal parameters between pipes,
4550
followed by the function body. They are stack-allocated and properly
@@ -55,10 +60,25 @@ stored in data structures or returned.
5560
}
5661
map_int({|x| x + 1 }, [1, 2, 3]);
5762

63+
The type of blocks is spelled `block(args) -> type`. Both closures and
64+
bare functions are automatically convert to `block`s when appropriate.
65+
Most higher-order functions should take their function arguments as
66+
`block`s.
67+
5868
A block with no arguments is written `{|| body(); }`—you can not leave
5969
off the pipes.
6070

61-
FIXME mention bind
71+
## Binding
72+
73+
Partial application is done using the `bind` keyword in Rust.
74+
75+
let daynum = bind std::vec::position(_, ["mo", "tu", "we", "do",
76+
"fr", "sa", "su"]);
77+
78+
Binding a function produces a closure (`lambda` type) in which some of
79+
the arguments to the bound function have already been provided.
80+
`daynum` will be a function taking a single string argument, and
81+
returning the day of the week that string corresponds to (if any).
6282

6383
## Iteration
6484

doc/tutorial/lib/codemirror-rust.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ CodeMirror.defineMode("rust", function() {
88
"lambda": "fn", "type": "type", "tag": "tag", "mod": "mod",
99
"as": "op", "true": "atom", "false": "atom", "assert": "op", "check": "op",
1010
"claim": "op", "native": "ignore", "unsafe": "ignore", "import": "else-style",
11-
"export": "else-style", "copy": "op", "log": "op", "log_err": "op", "use": "op"
11+
"export": "else-style", "copy": "op", "log": "op", "log_err": "op",
12+
"use": "op", "bind": "op"
1213
};
1314
var typeKeywords = function() {
1415
var keywords = {"fn": "fn", "block": "fn", "obj": "obj"};

doc/tutorial/syntax.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,31 +130,31 @@ annotation:
130130
The basic types are written like this:
131131

132132
`()`
133-
: Nil, the type that has only a single value.
133+
: Nil, the type that has only a single value.
134134

135135
`bool`
136-
: Boolean type..
136+
: Boolean type..
137137

138138
`int`
139-
: A machine-pointer-sized integer.
139+
: A machine-pointer-sized integer.
140140

141141
`uint`
142-
: A machine-pointer-sized unsigned integer.
142+
: A machine-pointer-sized unsigned integer.
143143

144144
`i8`, `i16`, `i32`, `i64`
145-
: Signed integers with a specific size (in bits).
145+
: Signed integers with a specific size (in bits).
146146

147147
`u8`, `u16`, `u32`, `u64`
148-
: Unsigned integers with a specific size.
148+
: Unsigned integers with a specific size.
149149

150150
`f32`, `f64`
151-
: Floating-point types.
151+
: Floating-point types.
152152

153153
`float`
154-
: The largest floating-point type efficiently supported on the target machine.
154+
: The largest floating-point type efficiently supported on the target machine.
155155

156156
`char`
157-
: A character is a 32-bit Unicode code point.
157+
: A character is a 32-bit Unicode code point.
158158

159159
`str`
160160
: String type. A string contains a utf-8 encoded sequence of characters.
@@ -163,22 +163,22 @@ These can be combined in composite types, which will be described in
163163
more detail later on (the `T`s here stand for any other type):
164164

165165
`[T]`
166-
: Vector type.
166+
: Vector type.
167167

168168
`[mutable T]`
169-
: Mutable vector type.
169+
: Mutable vector type.
170170

171171
`(T1, T2)`
172-
: Tuple type. Any arity above 1 is supported.
172+
: Tuple type. Any arity above 1 is supported.
173173

174174
`{fname1: T1, fname2: T2}`
175-
: Record type.
175+
: Record type.
176176

177-
`fn(arg1: T1, arg2: T2) -> T3`
178-
: Function type.
177+
`fn(arg1: T1, arg2: T2) -> T3`, `lambda()`, `block()`
178+
: Function types.
179179

180180
`@T`, `~T`, `*T`
181-
: Pointer types.
181+
: Pointer types.
182182

183183
`obj { fn method1() }`
184184
: Object type.

0 commit comments

Comments
 (0)