Skip to content

Commit 382143a

Browse files
committed
doc/rust.md: Demonstrate the f::<T>() syntax more often
The "Generic functions" subsection mentions that generic functions are instantiated based on context, so let's also mention right away (with a link to the #paths section) that an explicit form is available. This also adds an example to the function call expression section that explicitly instantiates a generic function.
1 parent cab8ec2 commit 382143a

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

doc/rust.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,11 @@ function defined above on `[1, 2]` will instantiate type parameter `T`
908908
with `int`, and require the closure parameter to have type
909909
`fn(int)`.
910910

911+
The type parameters can also be explicitly supplied in a trailing
912+
[path](#paths) component after the function name. This might be necessary
913+
if there is not sufficient context to determine the type parameters. For
914+
example, `sys::size_of::<u32>() == 4`.
915+
911916
Since a parameter type is opaque to the generic function, the set of
912917
operations that can be performed on it is limited. Values of parameter
913918
type can always be moved, but they can only be copied when the
@@ -2040,12 +2045,14 @@ an optional reference slot to serve as the function's output, bound to the
20402045
`lval` on the right hand side of the call. If the function eventually returns,
20412046
then the expression completes.
20422047

2043-
An example of a call expression:
2048+
Some examples of call expressions:
20442049

20452050
~~~~
20462051
# fn add(x: int, y: int) -> int { 0 }
2052+
# use core::from_str::FromStr::from_str;
20472053
20482054
let x: int = add(1, 2);
2055+
let pi = from_str::<f32>("3.14");
20492056
~~~~
20502057

20512058
### Lambda expressions

0 commit comments

Comments
 (0)