RustStructures


Syntax

  • struct Foo { field1: Type1, field2: Type2 }
  • let foo = Foo { field1: Type1::new(), field2: Type2::new() };
  • struct Bar ( Type1, Type2 ); // tuple type
  • let _ = Bar(Type1::new(), Type2::new());
  • struct Baz; // unit-like type
  • let _ = Baz;
  • let Foo { field1, .. } = foo; // extract field1 by pattern matching
  • let Foo { field1: x, .. } = foo; // extract field1 as x
  • let foo2 = Foo { field1: Type1::new(), .. foo }; // construct from existing
  • impl Foo { fn fiddle(&self) {} } // declare instance method for Foo
  • impl Foo { fn tweak(&mut self) {} } // declare mutable instance method for Foo
  • impl Foo { fn double(self) {} } // declare owning instance method for Foo
  • impl Foo { fn new() {} } // declare associated method for Foo