Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve documentation for Fn* traits #35810

Merged
merged 1 commit into from
Aug 30, 2016

Conversation

matthew-piziak
Copy link
Contributor

This PR is not yet a serious attempt at contribution. Rather, I'm opening this for discussion. I can think of a few things we may want to accomplish with the documentation of the Fn, FnMut, and FnOnce traits:

  • the relationship between these traits and the closures that implement them
  • examples of non-closure implementations
  • the relationship between these traits and Rust's ownership semantics

@rust-highfive
Copy link
Collaborator

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

@matthew-piziak
Copy link
Contributor Author

///
/// The `Fn` trait governs the invocation operator `()`. Closures automatically
/// implement this trait, which allows them to be invoked. Note, however, that
/// `Fn` takes an immutable reference to the argument. To take an immutable
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to write "to take an mutable"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do indeed. Thank you! I'll go ahead and refactor these docs a bit now.

@matthew-piziak
Copy link
Contributor Author

I've added some more documentation, more for form then contact at this point because I don't quite understand these traits yet.

@@ -64,6 +64,50 @@
//!
//! See the documentation for each trait for a minimum implementation that
//! prints something to the screen.
//!
//! The `Fn`, `FnMut`, and `FnOnce` traits are implemented by types that can be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should link to the traits

@matthew-piziak
Copy link
Contributor Author

This is now approaching a serious attempt at contribution. :P

@GuillaumeGomez
Copy link
Member

Nice! Seems good to me but I'll let @steveklabnik handles it.

@matthew-piziak
Copy link
Contributor Author

Actually, I believe my FnOnce example is still incorrect because I can change the parameter bound to FnMut and it still works.

@matthew-piziak
Copy link
Contributor Author

Okay, fixed.

@matthew-piziak
Copy link
Contributor Author

r? @steveklabnik

@bors
Copy link
Contributor

bors commented Aug 20, 2016

☔ The latest upstream changes (presumably #35857) made this pull request unmergeable. Please resolve the merge conflicts.

//! func(1)
//! }
//!
//! let double = |x| x + x;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps x * 2? I associate doubling with multiplication (though the two are clearly equivalent).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. :)

I can think of a few things we may want to accomplish with the documentation of the `Fn`, `FnMut`, and `FnOnce` traits:
- the relationship between these traits and the closures that implement them
- examples of non-closure implementations
- the relationship between these traits and Rust's ownership semantics

add module-level documentation for `Fn*` traits

Describe how `Fn*` traits, closure types, and ownership semantics are
linked, and provide examples of higher-level functions that take `Fn*`s.

more examples for `Fn*` traits

create correct (though not yet elegant) examples for `FnMut` and `FnOnce`

add trait links to module-level documentation

third time's a charm!

argument -> capture for trait documentation

This wording will need to be supported with better examples for
capturing eventually.

correct `FnOnce` example

I also fixed some of the trait wording here to make the concept of
capturing clearer; though that still needs more work.

replace `x + x` with `x * 2` for `fn double`
@steveklabnik
Copy link
Member

@bors: r+ rollup

thanks a lot!

@bors
Copy link
Contributor

bors commented Aug 25, 2016

📌 Commit 67b9cd3 has been approved by steveklabnik

@bors
Copy link
Contributor

bors commented Aug 29, 2016

⌛ Testing commit 67b9cd3 with merge 78f057b...

@bors
Copy link
Contributor

bors commented Aug 29, 2016

💔 Test failed - auto-mac-64-opt-rustbuild

@arielb1
Copy link
Contributor

arielb1 commented Aug 29, 2016

@bors retry

bors added a commit that referenced this pull request Aug 29, 2016
bors added a commit that referenced this pull request Aug 30, 2016
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 30, 2016
…steveklabnik

improve documentation for `Fn*` traits

This PR is not yet a serious attempt at contribution. Rather, I'm opening this for discussion. I can think of a few things we may want to accomplish with the documentation of the `Fn`, `FnMut`, and `FnOnce` traits:
- the relationship between these traits and the closures that implement them
- examples of non-closure implementations
- the relationship between these traits and Rust's ownership semantics
bors added a commit that referenced this pull request Aug 30, 2016
@bors bors merged commit 67b9cd3 into rust-lang:master Aug 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants