September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
30
Task/Classes/Rust/classes.rust
Normal file
30
Task/Classes/Rust/classes.rust
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
struct MyClass {
|
||||
variable: i32, // member variable = instance variable
|
||||
}
|
||||
|
||||
impl MyClass {
|
||||
// member function = method, with its implementation
|
||||
fn some_method(&mut self) {
|
||||
self.variable = 1;
|
||||
}
|
||||
|
||||
// constructor, with its implementation
|
||||
fn new() -> MyClass {
|
||||
// Here could be more code.
|
||||
MyClass { variable: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
fn main () {
|
||||
// Create an instance in the stack.
|
||||
let mut instance = MyClass::new();
|
||||
|
||||
// Create an instance in the heap.
|
||||
let mut p_instance = Box::<_>::new(MyClass::new());
|
||||
|
||||
// Invoke method on both istances,
|
||||
instance.some_method();
|
||||
p_instance.some_method();
|
||||
|
||||
// Both instances are automatically deleted when their scope ends.
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue