Consuming Self
consuming_self.nr80 lines
consuming_self.nr· neuro
// Consuming `self` receivers. A method takes its receiver three ways: `&self` borrows it,
// `&mut self` borrows it exclusively, and bare `self` consumes it. After a consuming call
// the caller no longer has the value, as with a by-value argument. `into_*` conversions
// need this: the method owns the fields and can move a non-`Copy` one out without cloning
// it.
struct Point {
x: f64,
y: f64
}
impl Point {
func new(x: f64, y: f64) -> Point {
Point { x: x, y: y }
}
// `&self` borrows: the caller keeps the point.
func length_squared(&self) -> f64 {
self.x * self.x + self.y * self.y
}
// `self` consumes: the caller does not.
func into_tuple(self) -> (f64, f64) {
(self.x, self.y)
}
}
// A non-Copy struct: `string` owns a buffer, so `Label` moves rather than copies.
struct Label {
text: string
}
impl Label {
// The receiver's field is moved out, not cloned. Only a consuming receiver
// may do this: a `&self` body would release a buffer its caller still owns.
func into_text(self) -> string {
self.text
}
}
// A `Drop` type proves WHERE the destructor runs. The callee owns what it was
// handed, so it destroys the receiver at its own exit: once, never twice.
struct Ticket {
spent: &mut i32
}
impl Drop for Ticket {
func drop(&mut self) {
*self.spent = *self.spent + 1
}
}
impl Ticket {
func redeem(self) -> i32 {
7
}
}
func main() -> i32 {
val p = Point::new(3.0, 4.0)
val squared = p.length_squared() // borrows: `p` survives
val coords = p.into_tuple() // moves: `p` is gone after this
// val again = p.length_squared() // <- rejected: use of moved value 'p'
println("length_squared = {squared}")
println("coords = {coords.0} {coords.1}")
val label = Label { text: "sensor-a" }
val text = label.into_text()
println("label text = {text}")
mut spent: i32 = 0
val value = Ticket { spent: &mut spent }.redeem()
println("ticket value = {value}")
println("tickets spent = {spent}")
// 25 + 7 + 1 = 33
val code = squared as i32 + value + spent
println("packed code = {code}")
return code
}
neuroc run consuming_self.nr
length_squared = 25.0
coords = 3.0 4.0
label text = sensor-a
ticket value = 7
tickets spent = 1
packed code = 33