In Rust, methods and associated functions are both defined using impl
blocks. While they are similar in some ways, they serve different purposes:
self
parameter.StructName::function_name()
.self
parameter (&self
, &mut self
, or self
).instance.method_name()
.In the previous challenge, the Logger::log_message
was an associated function because it didn't take self
as a parameter. In this challenge, you will learn how to create methods that operate on struct instances.
Define a struct Counter
that represents a simple counter. Implement methods on this struct to increment, decrement, and get the current count. This will help you understand how to define and use methods that operate on a struct instance.
Define a struct Counter
with a single field count
of type i32
.
Define a new
associated function that acts as a constructor and initializes the count
field to 0.
Implement the following methods for Counter
:
increment
: Increments the counter by 1.decrement
: Decrements the counter by 1.get_count
: Returns the current count.Ensure these methods use the correct self
parameter type (&self
or &mut self
) based on their behavior.
Make the count
field private and provide a public constructor Counter::new
that initializes the count to 0.
let mut counter = Counter::new();
counter.increment();
counter.increment();
assert_eq!(counter.get_count(), 2);
counter.decrement();
assert_eq!(counter.get_count(), 1);
If you're stuck, you can check out these hints:
increment
and decrement
methods, use &mut self
as the parameter type.self.count += 1
to modify the counter in the increment
method.self.count -= 1
to modify the counter in the decrement
method.get_count
, use &self
and return the value of self.count
.// 1. Define the structpub struct Counter{ pub count: i32}// 2. Implement the associated function and methodsimpl Counter{ pub fn new()-> Self{ Self{ count: 0 } } pub fn increment(&mut self){ self.count+= 1; } pub fn decrement(&mut self){ self.count-=1; } pub fn get_count(&self) -> i32{ self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Self { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { pub count: i32,}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter { count: 0 } } pub fn increment(&mut self) { self.count += 1 } pub fn decrement(&mut self) { self.count -= 1 } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Self { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { return self.count; }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { counter: i64,}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Self { counter: 0 } } pub fn increment(&mut self) { self.counter += 1; } pub fn decrement(&mut self) { self.counter -= 1; } pub fn get_count(&self) -> i64 { self.counter }}impl Default for Counter { fn default() -> Self { Self::new() }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32,}impl Counter{ pub fn new()->Counter{ Counter{ count: 0, } } pub fn increment(&mut self){ self.count += 1 } pub fn decrement(&mut self){ self.count -= 1 } pub fn get_count(&self)->i32{ self.count } }pub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { pub count: i32,}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter { count: 0, } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32,}impl Counter{ pub fn new()->Counter{ Counter{ count: 0, } } pub fn increment(&mut self){ self.count += 1 } pub fn decrement(&mut self){ self.count -= 1 } pub fn get_count(&self)->i32{ self.count } }pub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter{ count: i32}// 2. Implement the associated function and methodsimpl Counter{ pub fn new() -> Self{ Counter{count: 0} } pub fn increment(&mut self){ self.count += 1; } pub fn decrement(&mut self){ self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter{ count:i32,}// 2. Implement the associated function and methodsimpl Counter{ pub fn new()-> Counter { Counter{ count : 0, } } pub fn increment(&mut self) { self.count +=1 } pub fn decrement(&mut self){ self.count -= 1 } pub fn get_count(&self) -> i32{ self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter{ count: i32,}// 2. Implement the associated function and methodsimpl Counter{ pub fn new() -> Counter{ Counter{ count: 0, } } pub fn increment(&mut self){ self.count += 1; } pub fn decrement(&mut self){ self.count -= 1; } pub fn get_count(&self) -> i32{ self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32,}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter { count: 0 } } pub fn get_count(&self) -> i32 { self.count } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { pub count: i32,}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Counter { count: 0, } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count } }// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { pub count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter { count : 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32{ self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { pub count: i32}impl Counter { pub fn new() -> Counter{ Counter{ count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32{ self.count }}// 2. Implement the associated function and methods// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Counter { count: 0 } } pub fn increment(&mut self) { self.count +=1; } pub fn decrement(&mut self) { self.count -=1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}impl Counter { pub fn new() -> Self { Counter{count: 0} } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// 2. Implement the associated function and methods// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}impl Counter { pub fn new() -> Self { Counter{count: 0} } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// 2. Implement the associated function and methods// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter { count: 0 } } pub fn increment(& mut self) { self.count = &self.count + 1 } pub fn decrement(& mut self) { self.count = &self.count - 1 } pub fn get_count(&self)-> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
pub struct Counter { count: i32}impl Counter { pub fn new() -> Counter { Counter{count: 0} } pub fn increment(&mut self) { self.count += 1 } pub fn decrement(&mut self) { self.count -= 1 } pub fn get_count(&self) -> i32 { self.count }}pub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { return Counter { count: 0 }; } pub fn get_count(&self) -> i32 { return self.count; } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32,}// 2. Implement the associated function and methodsimpl Counter { // Associated function to create a new Counter instance pub fn new() -> Self { Counter { count: 0 } } // Method to increment the counter by 1 pub fn increment(&mut self) { self.count += 1; } // Method to decrement the counter by 1 pub fn decrement(&mut self) { self.count -= 1; } // Method to get the current count pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32,}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Self {count: 0} } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
pub struct Counter { count: i32,}impl Counter { pub fn new() -> Counter { Counter{ count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
pub struct Counter { count: i32}impl Counter { pub fn new() -> Self { Counter { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter{ count: i32,}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { return self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}impl Counter { pub fn increment(&mut self) { self.count += 1 } pub fn decrement(&mut self) { self.count -= 1 } pub fn get_count(&self) -> i32 { self.count } pub fn new() -> Counter { Counter { count: 0 } }}// 2. Implement the associated function and methods// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
pub struct Counter { pub count: i32}impl Counter { pub fn new() -> Counter{ Counter { count: 0 } } pub fn increment(&mut self) { self.count+=1; } pub fn decrement(&mut self) { self.count -=1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
pub struct Counter { pub count: i32}impl Counter { pub fn new() -> Counter{ Counter { count: 0 } } pub fn increment(&mut self) { self.count+=1; } pub fn decrement(&mut self) { self.count -=1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { pub count : i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter { count : 0 } } pub fn increment(&mut self) { self.count+= 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { pub count:i32}impl Counter { pub fn new()-> Counter { Counter { count:0 } } pub fn increment(&mut self) { self.count+=1 } pub fn decrement(&mut self) { self.count-=1 } pub fn get_count(&self)->i32 { self.count }}// 2. Implement the associated function and methods// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32,}impl Counter { pub fn new() -> Counter { Counter { count: 0 } } pub fn increment(&mut self) { self.count += 1 } pub fn decrement(&mut self) { self.count -= 1 } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { value: i32,}impl Counter { pub fn new() -> Self { Counter { value:0 } } pub fn increment(&mut self) { self.value += 1; } pub fn decrement(&mut self) { self.value -= 1; } pub fn get_count(&self) -> i32 { self.value }}// 2. Implement the associated function and methods// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Counter { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { value: i32,}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Self { value: 0 } } pub fn increment(&mut self) { self.value += 1; } pub fn decrement(&mut self) { self.value -= 1; } pub fn get_count(&self) -> i32 { self.value }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32,}impl Counter { pub fn new() -> Counter { Counter { count: 0, } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter{ status: isize,}impl Counter { pub fn increment(&mut self) -> () { self.status += 1; } pub fn decrement(&mut self) -> () { self.status -= 1; } pub fn get_count(&self) -> isize { self.status } pub fn new() -> Counter { Counter{ status: 0, } }}// 2. Implement the associated function and methods// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { pub count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Counter { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}impl Counter { pub fn new() -> Self { Counter { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// 2. Implement the associated function and methods// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
pub struct Counter { pub count: i32,}impl Counter { pub fn new() -> Self { Self { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}pub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { pub count: i32,}impl Counter { pub fn new() -> Counter { Counter{ count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// 2. Implement the associated function and methods// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}impl Counter { pub fn new() -> Counter { Counter{ count: 0 } } pub fn get_count(&self) -> i32 { self.count } pub fn increment(&mut self) { self.count += 1 } pub fn decrement(&mut self) { self.count -= 1 }}// 2. Implement the associated function and methods// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter{ count : i32,}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter{ Counter { count : 0, } } pub fn increment (&mut self) { self.count += 1; } pub fn decrement (&mut self) { self.count -= 1; } pub fn get_count (&self) -> i32{ self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter {count: 0} } pub fn increment(&mut self) { self.count += 1 } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter{ val : i32}// 2. Implement the associated function and methodsimpl Counter{ pub fn new () -> Self { Counter { val: 0 } } pub fn increment(&mut self) { self.val += 1; } pub fn decrement(&mut self) { self.val -= 1; } pub fn get_count(&self) -> i32 { return self.val; }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter(i32);// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Counter(0) } pub fn get_count(&self) -> i32 { self.0 } pub fn increment(&mut self) { self.0 += 1; } pub fn decrement(&mut self) { self.0 -= 1; }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Self { Self {count: 0} } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}
// 1. Define the structpub struct Counter { count: i32,}// 2. Implement the associated function and methodsimpl Counter { pub fn new() -> Counter { Counter { count: 0 } } pub fn increment(&mut self) { self.count += 1; } pub fn decrement(&mut self) { self.count -= 1; } pub fn get_count(&self) -> i32 { self.count }}// Example use casepub fn main() { let mut counter = Counter::new(); counter.increment(); assert_eq!(counter.get_count(), 1); counter.increment(); counter.increment(); assert_eq!(counter.get_count(), 3); counter.decrement(); assert_eq!(counter.get_count(), 2); counter.decrement(); counter.decrement(); assert_eq!(counter.get_count(), 0);}