-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct.rs
More file actions
57 lines (46 loc) · 1.42 KB
/
Copy pathstruct.rs
File metadata and controls
57 lines (46 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// struct User{
// active: bool,
// username: String,
// age: i8,
// sign_in_count: i32,
// }
// fn main(){
// let user1 = User {
// active : true,
// username : String::from("some username"),
// age : 30,
// sign_in_count : 10,
// };
// println!("User1 username: {:?}",user1.username);
// }
//Implementing Structs
struct Rect{
width: i32,
height: i32,
}
impl Rect{
// this is like the methods inside the class in JS
// &self is like the this keyword, it contains the Struct which is currently being used.
fn area(&self) -> i32{
self.width * self.height
//return self.width * self.height;
}
fn perimeter(&self,num: i32) -> i32{
2*(self.width+self.height)
}
//static functions : functions without arguments, and these are called using Structs and not their objects.
fn debug() -> i32{
return 1;
}
}
fn main(){
let rect1 = Rect{
width: 12,
height: 10,
};
//We don't have to mention self as paramter as it is already reserved as the first parameter.
println!("The area of the rectangle is: {}",rect1.area());
// Here the first argument is already reserved for self and hence it only takes the second argument which is the number 1.
println!("the perimeter func takes 2 arguments {}",rect1.perimeter(1));
println!("static function debug: {}",Rect::debug());
}