Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 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