Rust methods: Difference between revisions
From wikinotes
No edit summary |
No edit summary |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Rust lets you add methods to structs. | Rust lets you add methods to <code>structs</code> and <code>enums</code>. | ||
{{ TODO | | {{ TODO | | ||
other types? what's the full spectrum here? | other types? what's the full spectrum here? | ||
}} | }} | ||
= | = Associated Functions = | ||
<blockquote> | <blockquote> | ||
Rust lets you add methods to structs | Rust lets you add methods to structs/enums using <code>impl</code>. | ||
* <code>self</code> is a reference to the instance | |||
* <code>Self</code> is a reference to the object-type | |||
* <code>impl</code> allows you to associate functions to an object | |||
* you can have as many <code>impl</code> blocks as you'd like | |||
<syntaxhighlight lang="rust"> | <syntaxhighlight lang="rust"> | ||
Line 17: | Line 21: | ||
impl RgbColor { | impl RgbColor { | ||
// ~static-method | |||
fn red() -> Self { | |||
Self { | |||
r: 255, | |||
g: 0, | |||
b: 0, | |||
} | |||
} | |||
// method | |||
fn brighten(&self) { | fn brighten(&self) { | ||
self.r += 8; | self.r += 8; | ||
Line 24: | Line 38: | ||
} | } | ||
// methods | |||
let c = RgbColor{r: 8, g: 8, b: 8}; | let c = RgbColor{r: 8, g: 8, b: 8}; | ||
c.brighen(); // {r: 16, g: 16, b: 16} | c.brighen(); // {r: 16, g: 16, b: 16} | ||
// class methods | |||
red = RgbColor::red() | |||
</syntaxhighlight> | |||
Similar to go, you can use the <code>.</code> operator to access methods on either a reference or an instance.<br> | |||
rust will automatically dereference it for you. | |||
</blockquote><!-- Associated Functions --> | |||
= Traits = | |||
<blockquote> | |||
You can enforce that types implement a method,<br> | |||
and even provide a default method for them using traits. | |||
See more details in [[rust traits]]. | |||
<syntaxhighlight lang="rust"> | |||
struct Cat { name: String } | |||
struct Dog { name: String, breed: String } | |||
// all implementors of 'Pet' must have method 'play' with this method signature | |||
trait Pet { | |||
fn play(&self) -> bool; | |||
} | |||
impl Pet for Cat { | |||
fn play(&self) -> bool { | |||
println!("you give a ball of yarn to {}", self.name); | |||
true | |||
} | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- | </blockquote><!-- Traits --> |
Latest revision as of 23:24, 8 February 2023
Rust lets you add methods to structs
and enums
.
TODO:
other types? what's the full spectrum here?
Associated Functions
Rust lets you add methods to structs/enums using
impl
.
self
is a reference to the instanceSelf
is a reference to the object-typeimpl
allows you to associate functions to an object- you can have as many
impl
blocks as you'd liketype RgbColor{ r: u8, g: u8, b: u8, } impl RgbColor { // ~static-method fn red() -> Self { Self { r: 255, g: 0, b: 0, } } // method fn brighten(&self) { self.r += 8; self.g += 8; self.b += 8; } } // methods let c = RgbColor{r: 8, g: 8, b: 8}; c.brighen(); // {r: 16, g: 16, b: 16} // class methods red = RgbColor::red()Similar to go, you can use the
.
operator to access methods on either a reference or an instance.
rust will automatically dereference it for you.
Traits
You can enforce that types implement a method,
and even provide a default method for them using traits.See more details in rust traits.
struct Cat { name: String } struct Dog { name: String, breed: String } // all implementors of 'Pet' must have method 'play' with this method signature trait Pet { fn play(&self) -> bool; } impl Pet for Cat { fn play(&self) -> bool { println!("you give a ball of yarn to {}", self.name); true } }