Skip to content

Commit 09df3ed

Browse files
committed
docs: Explain a little bit about dtors
1 parent 8fc60af commit 09df3ed

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

doc/tutorial.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ omitted from the type, such an assignment would result in a type error.
734734

735735
Structs can be destructured in `match` patterns. The basic syntax is
736736
`Name {fieldname: pattern, ...}`:
737+
737738
~~~~
738739
# struct Point { x: float, y: float }
739740
# let mypoint = Point { x: 0.0, y: 0.0 };
@@ -747,6 +748,35 @@ In general, the field names of a struct do not have to appear in the same
747748
order they appear in the type. When you are not interested in all
748749
the fields of a struct, a struct pattern may end with `, _` (as in
749750
`Name {field1, _}`) to indicate that you're ignoring all other fields.
751+
Additionally, struct fields have a shorthand matching form that simply
752+
reuses the field name as the binding name.
753+
754+
~~~
755+
# struct Point { x: float, y: float }
756+
# let mypoint = Point { x: 0.0, y: 0.0 };
757+
match mypoint {
758+
Point { x, _ } => { io::println(x.to_str()) }
759+
}
760+
~~~
761+
762+
Structs are the only type in Rust that may have user-defined destructors,
763+
using `drop` blocks, inside of which the struct's value may be referred
764+
to with the name `self`.
765+
766+
~~~
767+
struct TimeBomb {
768+
explosivity: uint,
769+
770+
drop {
771+
for iter::repeat(explosivity) {
772+
io::println(fmt!("blam!"));
773+
}
774+
}
775+
}
776+
~~~
777+
778+
> ***Note***: This destructor syntax is temporary. Eventually destructors
779+
> will be defined for any type using [traits](#traits).
750780
751781
## Enums
752782

0 commit comments

Comments
 (0)