Tutorial by Examples

Structures in Rust are defined using the struct keyword. The most common form of structure consists of a set of named fields: struct Foo { my_bool: bool, my_num: isize, my_string: String, } The above declares a struct with three fields: my_bool, my_num, and my_string, of the type...
Consider the following struct definitions: struct Foo { my_bool: bool, my_num: isize, my_string: String, } struct Bar (bool, isize, String); struct Baz; Constructing new structure values for these types is straightforward: let foo = Foo { my_bool: true, my_num: 42, my_string: ...
To declare methods on a struct (i.e., functions that can be called "on" the struct, or values of that struct type), create an impl block: impl Foo { fn fiddle(&self) { // "self" refers to the value this method is being called on println!("fiddling...
Structures can be made generic over one or more type parameters. These types are given enclosed in <> when referring to the type: struct Gen<T> { x: T, z: isize, } // ... let _: Gen<bool> = Gen{x: true, z: 1}; let _: Gen<isize> = Gen{x: 42, z: 2}; let _: Gen...

Page 1 of 1