@@ -1792,6 +1792,85 @@ fn contains(v: ~[int], elt: int) -> bool {
1792
1792
1793
1793
`for` syntax only works with stack closures.
1794
1794
1795
+ # Classes
1796
+
1797
+ Rust lets users define new types with fields and methods, called 'classes', in
1798
+ the style of object-oriented languages.
1799
+
1800
+ (Warning: Rust's classes are in the process of changing rapidly. Some more
1801
+ information about some of the potential changes is [here][classchanges].)
1802
+
1803
+ [classchanges]: http://pcwalton.github.com/blog/2012/06/03/maximally-minimal-classes-for-rust/
1804
+
1805
+ An example of a class:
1806
+
1807
+ ~~~~
1808
+ class example {
1809
+ let mut x: int;
1810
+ let y: int;
1811
+
1812
+ priv {
1813
+ let mut private_member: int;
1814
+ fn private_method() {}
1815
+ }
1816
+
1817
+ new(x: int) {
1818
+ // Constructor
1819
+ self.x = x;
1820
+ self.y = 7;
1821
+ self.a();
1822
+ }
1823
+
1824
+ fn a() {
1825
+ io::println("a");
1826
+ }
1827
+
1828
+ drop {
1829
+ // Destructor
1830
+ self.x = 0;
1831
+ }
1832
+ }
1833
+
1834
+ fn main() {
1835
+ let x: example = example(1);
1836
+ let y: @example = @example(2);
1837
+ x.a();
1838
+ x.x = 5;
1839
+ }
1840
+ ~~~~
1841
+
1842
+ Fields and methods are declared just like functions and local variables, using
1843
+ 'fn' and 'let'. As usual, 'let mut' can be used to create mutable fields. At
1844
+ minimum, Rust classes must have at least one field.
1845
+
1846
+ Rust classes must also have a constructor, and can optionally have a destructor
1847
+ as well. The constructor and destructor are declared as shown in the example:
1848
+ like methods named 'new' and 'drop', but without 'fn', and without arguments
1849
+ for drop.
1850
+
1851
+ In the constructor, the compiler will enforce that all fields are initialized
1852
+ before doing anything which might allow them to be accessed. This includes
1853
+ returning from the constructor, calling any method on 'self', calling any
1854
+ function with 'self' as an argument, or taking a reference to 'self'. Mutation
1855
+ of immutable fields is possible only in the constructor, and only before doing
1856
+ any of these things; afterwards it is an error.
1857
+
1858
+ Private fields and methods are declared as shown above, using a `priv { ... }`
1859
+ block within the class. They are accessible only from within the same instance
1860
+ of the same class. (For example, even from within class A, you cannot call
1861
+ private methods, or access private fields, on other instances of class A; only
1862
+ on `self`.) This accessibility restriction may change in the future.
1863
+
1864
+ As mentioned below, in the section on copying types, classes with destructors
1865
+ are considered 'resource' types and are not copyable.
1866
+
1867
+ Declaring a class also declares its constructor as a function of the same name.
1868
+ You can construct an instance of the class, as in the example, by calling that
1869
+ function. The function and the type, though they have the same name, are
1870
+ otherwise independent. As with other Rust types, you can use `@` or `~` to
1871
+ construct a heap-allocated instance of a class, either shared or unique; just
1872
+ call e.g. `@example(...)` as shown above.
1873
+
1795
1874
# Argument passing
1796
1875
1797
1876
Rust datatypes are not trivial to copy (the way, for example,
0 commit comments