Skip to content

Commit e6e8c47

Browse files
committed
Fix nomenclature around methods
Reported in https://twitter.com/georgeclaghorn/status/1414948486647328775 Rust has functions. Functions that are associated with a type are "associated functions." Associated functions that take self are a "method." Rust does not have "static methods" or "instance methods" or "objects." See https://doc.rust-lang.org/stable/reference/items/associated-items.html#associated-functions-and-methods for more.
1 parent a4b2516 commit e6e8c47

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/fn/methods.md

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
# Methods
1+
# Associated functions & Methods
22

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

78
```rust,editable
89
struct Point {
910
x: f64,
1011
y: f64,
1112
}
1213
13-
// Implementation block, all `Point` methods go in here
14+
// Implementation block, all `Point` associated functions & methods go in here
1415
impl Point {
15-
// This is a static method
16-
// Static methods don't need to be called by an instance
17-
// These methods are generally used as constructors
16+
// This is an "associated function" because this function is associated with
17+
// a particular type, that is, Point.
18+
//
19+
// Associated functions don't need to be called with an instance.
20+
// These functions are generally used like constructors.
1821
fn origin() -> Point {
1922
Point { x: 0.0, y: 0.0 }
2023
}
2124
22-
// Another static method, taking two arguments:
25+
// Another associated function, taking two arguments:
2326
fn new(x: f64, y: f64) -> Point {
2427
Point { x: x, y: y }
2528
}
@@ -31,7 +34,7 @@ struct Rectangle {
3134
}
3235
3336
impl Rectangle {
34-
// This is an instance method
37+
// This is a method
3538
// `&self` is sugar for `self: &Self`, where `Self` is the type of the
3639
// caller object. In this case `Self` = `Rectangle`
3740
fn area(&self) -> f64 {
@@ -80,12 +83,12 @@ impl Pair {
8083
8184
fn main() {
8285
let rectangle = Rectangle {
83-
// Static methods are called using double colons
86+
// Associated functions are called using double colons
8487
p1: Point::origin(),
8588
p2: Point::new(3.0, 4.0),
8689
};
8790
88-
// Instance methods are called using the dot operator
91+
// Methods are called using the dot operator
8992
// Note that the first argument `&self` is implicitly passed, i.e.
9093
// `rectangle.perimeter()` === `Rectangle::perimeter(&rectangle)`
9194
println!("Rectangle perimeter: {}", rectangle.perimeter());
@@ -112,4 +115,4 @@ fn main() {
112115
//pair.destroy();
113116
// TODO ^ Try uncommenting this line
114117
}
115-
```
118+
```

src/std_misc/file/create.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `create`
22

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

src/std_misc/file/open.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `open`
22

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

55
A `File` owns a resource, the file descriptor and takes care of closing the
66
file when it is `drop`ed.

src/trait.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ methods from `Animal` with a `Sheep`.
1212
struct Sheep { naked: bool, name: &'static str }
1313
1414
trait Animal {
15-
// Static method signature; `Self` refers to the implementor type.
15+
// Associated function signature; `Self` refers to the implementor type.
1616
fn new(name: &'static str) -> Self;
1717
18-
// Instance method signatures; these will return a string.
18+
// Method signatures; these will return a string.
1919
fn name(&self) -> &'static str;
2020
fn noise(&self) -> &'static str;
2121
@@ -77,4 +77,4 @@ fn main() {
7777
dolly.shear();
7878
dolly.talk();
7979
}
80-
```
80+
```

0 commit comments

Comments
 (0)