Skip to content

Commit a5c4a38

Browse files
committed
Expand fn keyword docs
1 parent f91ad44 commit a5c4a38

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

src/libstd/keyword_docs.rs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,21 +223,73 @@ mod extern_keyword { }
223223

224224
#[doc(keyword = "fn")]
225225
//
226-
/// The `fn` keyword.
226+
/// The keyword for defining functions.
227227
///
228-
/// The `fn` keyword is used to declare a function.
228+
/// Functions are the primary way code is executed within Rust. Function blocks, usually just
229+
/// called functions, can be defined in a variety of different places and be assigned many
230+
/// different attributes and modifiers.
229231
///
230-
/// Example:
232+
/// Standalone functions that just sit within a module not attached to anything else are common,
233+
/// but most functions will end up being inside [`impl`] blocks, either on another type itself, or
234+
/// as a trait impl for that type.
231235
///
232236
/// ```rust
233-
/// fn some_function() {
234-
/// // code goes in here
237+
/// fn standalone_function() {
238+
/// // code
239+
/// }
240+
///
241+
/// pub fn public_thing(argument: bool) -> String {
242+
/// // code
243+
/// # "".to_string()
244+
/// }
245+
///
246+
/// struct Thing {
247+
/// foo: i32,
248+
/// }
249+
///
250+
/// impl Thing {
251+
/// pub fn new() -> Self {
252+
/// Self {
253+
/// foo: 42,
254+
/// }
255+
/// }
235256
/// }
236257
/// ```
237258
///
238-
/// For more information about functions, take a look at the [Rust Book][book].
259+
/// See docs on [`impl`] and [`self`] for relevant details on those.
260+
///
261+
/// In addition to presenting fixed types in the form of `fn name(arg: type, ..) -> return_type`,
262+
/// functions can also declare a list of type parameters along with trait bounds that they fall
263+
/// into.
239264
///
240-
/// [book]: https://doc.rust-lang.org/book/second-edition/ch03-03-how-functions-work.html
265+
/// ```rust
266+
/// fn generic_function<T: Clone>(x: T) -> (T, T, T) {
267+
/// (x.clone(), x.clone(), x.clone())
268+
/// }
269+
///
270+
/// fn generic_where<T>(x: T) -> T
271+
/// where T: std::ops::Add<Output=T> + Copy
272+
/// {
273+
/// x + x + x
274+
/// }
275+
/// ```
276+
///
277+
/// Declaring trait bounds in the angle brackets is functionally identical to using a [`where`]
278+
/// clause, but `where` is preferred due to it being easier to understand at a glance.
279+
///
280+
/// Along with being made public via [`pub`], `fn` can also have an [`extern`] added for use in
281+
/// FFI.
282+
///
283+
/// For more information on the various types of functions and how they're used, consult the [Rust
284+
/// book] or the [Reference].
285+
///
286+
/// [`impl`]: keyword.impl.html
287+
/// [`self`]: keyword.self.html
288+
/// [`where`]: keyword.where.html
289+
/// [`pub`]: keyword.pub.html
290+
/// [`extern`]: keyword.extern.html
291+
/// [Rust book]: https://doc.rust-lang.org/book/second-edition/ch03-03-how-functions-work.html
292+
/// [Reference]: https://doc.rust-lang.org/reference/items/functions.html
241293
mod fn_keyword { }
242294

243295
#[doc(keyword = "let")]

0 commit comments

Comments
 (0)