Skip to content

Fix nomenclature around methods #1450

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

Merged
merged 1 commit into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/fn/methods.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
# Methods
# Associated functions & Methods

Methods are functions attached to objects. These methods have access to the
data of the object and its other methods via the `self` keyword. Methods are
defined under an `impl` block.
Some functions are connected to a particular type. These come in two forms:
associated functions, and methods. Associated functions are functions that
are defined on a type generally, while methods are associated functions that are
called on a particular instance of a type.

```rust,editable
struct Point {
x: f64,
y: f64,
}

// Implementation block, all `Point` methods go in here
// Implementation block, all `Point` associated functions & methods go in here
impl Point {
// This is a static method
// Static methods don't need to be called by an instance
// These methods are generally used as constructors
// This is an "associated function" because this function is associated with
// a particular type, that is, Point.
//
// Associated functions don't need to be called with an instance.
// These functions are generally used like constructors.
fn origin() -> Point {
Point { x: 0.0, y: 0.0 }
}

// Another static method, taking two arguments:
// Another associated function, taking two arguments:
fn new(x: f64, y: f64) -> Point {
Point { x: x, y: y }
}
Expand All @@ -31,7 +34,7 @@ struct Rectangle {
}

impl Rectangle {
// This is an instance method
// This is a method
// `&self` is sugar for `self: &Self`, where `Self` is the type of the
// caller object. In this case `Self` = `Rectangle`
fn area(&self) -> f64 {
Expand Down Expand Up @@ -80,12 +83,12 @@ impl Pair {

fn main() {
let rectangle = Rectangle {
// Static methods are called using double colons
// Associated functions are called using double colons
p1: Point::origin(),
p2: Point::new(3.0, 4.0),
};

// Instance methods are called using the dot operator
// Methods are called using the dot operator
// Note that the first argument `&self` is implicitly passed, i.e.
// `rectangle.perimeter()` === `Rectangle::perimeter(&rectangle)`
println!("Rectangle perimeter: {}", rectangle.perimeter());
Expand All @@ -112,4 +115,4 @@ fn main() {
//pair.destroy();
// TODO ^ Try uncommenting this line
}
```
```
2 changes: 1 addition & 1 deletion src/std_misc/file/create.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `create`

The `create` static method opens a file in write-only mode. If the file
The `create` function opens a file in write-only mode. If the file
already existed, the old content is destroyed. Otherwise, a new file is
created.

Expand Down
2 changes: 1 addition & 1 deletion src/std_misc/file/open.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `open`

The `open` static method can be used to open a file in read-only mode.
The `open` function can be used to open a file in read-only mode.

A `File` owns a resource, the file descriptor and takes care of closing the
file when it is `drop`ed.
Expand Down
6 changes: 3 additions & 3 deletions src/trait.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ methods from `Animal` with a `Sheep`.
struct Sheep { naked: bool, name: &'static str }

trait Animal {
// Static method signature; `Self` refers to the implementor type.
// Associated function signature; `Self` refers to the implementor type.
fn new(name: &'static str) -> Self;

// Instance method signatures; these will return a string.
// Method signatures; these will return a string.
fn name(&self) -> &'static str;
fn noise(&self) -> &'static str;

Expand Down Expand Up @@ -77,4 +77,4 @@ fn main() {
dolly.shear();
dolly.talk();
}
```
```