-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructs2.rs
More file actions
65 lines (52 loc) · 1.15 KB
/
Copy pathStructs2.rs
File metadata and controls
65 lines (52 loc) · 1.15 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
58
59
60
61
62
63
64
65
/*
Structures and impl blocks
*/
struct Car{
owner: String,
price: u32,
fuel: f32,
milage: f32,
}
impl Car{
fn installment(&self)-> u32{
100
}
fn selling_price(&mut self) -> u32{
self.price + Car::installment(self)
}
fn display(&self){
println!("{} {} {} {}", self.owner, self.price, self.fuel, self.milage);
}
fn refuel(&mut self, liter:f32){
self.fuel += liter;
}
fn sell(self) -> Self{
self
}
fn new(oname:String, price:u32, milage:f32) -> Car {
let car:Car = Car{
owner: oname,
price: price,
fuel: 0.0,
milage: milage,
};
car
}
}
fn main(){
let mut newcar:Car = Car{
owner: String::from("RamLal"),
price: 2_000,
fuel: 21.5,
milage: 10.2,
};
newcar.display();
newcar.refuel(10.20);
newcar.display();
let new_price = newcar.selling_price();
println!("{}",new_price);
let chnage_ower = newcar.sell();
//newcar.refuel(20.0);
let another_car = Car::new("Maruti".to_string(), 10, 100.0);
another_car.display();
}