Skip to content

Commit 10faa52

Browse files
committed
auto merge of #5188 : ben0x539/rust/doc-call-generic-fn, r=catamorphism
I have seen a few people confused on how to explicitly instantiate generic functions, since the syntax differs from C++'s and C#'s, which is probably where most people asking questions about generic functions are coming from. The only use of the `::<T>` syntax in the reference right now is in the section on paths, which is possibly not where someone trying to find out about generic functions is going to start looking. The tutorial doesn't mention it at all, but I think it's all right to make the reference a tiny bit more redundant and avoid stuffing the tutorial with syntax details. ---- 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 that explicitly instantiates a generic function to the function call expression section.
2 parents 0917e13 + 382143a commit 10faa52

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
@@ -2055,12 +2060,14 @@ an optional reference slot to serve as the function's output, bound to the
20552060
`lval` on the right hand side of the call. If the function eventually returns,
20562061
then the expression completes.
20572062

2058-
An example of a call expression:
2063+
Some examples of call expressions:
20592064

20602065
~~~~
20612066
# fn add(x: int, y: int) -> int { 0 }
2067+
# use core::from_str::FromStr::from_str;
20622068
20632069
let x: int = add(1, 2);
2070+
let pi = from_str::<f32>("3.14");
20642071
~~~~
20652072

20662073
### Lambda expressions

0 commit comments

Comments
 (0)