Hi, guys
Happy new year
I'm having some trouble refactoring my code. Coming from OOP, it's been hard to resist the temptation to factor methods that belong together into different classes.
What I'm actually trying to do is in a reply below, this is what a reduced case would look like
struct test1
{
a:bool
}
impl test1
{
pub fn new() -> test1
{
test1{a:true}
}
pub fn fn1(&mut self) -> &str
{
"hi"
}
pub fn fn2(&self) -> &str
{
//I'd like to do something like mystate+=1 here so another method can perform operation depending on mystate
"bye"
}
}
struct test
{
object: test1
}
impl test
{
pub fn new() -> test
{
test
{
object:test1::new()
}
}
fn fn2(&self, a: &str)
{}
pub fn useTest2(&mut self)
{
let a = self.object.fn1();
self.fn2(a); //This is where rust doesn't let me use self again because it already has been borrowed
}
}
fn main()
{
let t = test::new();
t.useTest2();
}
While I understand (very poorly) that self has been borrowed by object, so object follows the same lifetime as self so I can't borrow it again, I can't find a solution other than maintaining the state I wanted to maintain in struct test, which I'd like to avoid.
So for now a specific question is: How would you re-factor this?
And a more generic question is, is way of organizing code discouraged in rust?
I'm thinking in C# I could do something like
//probably not the best example because none of these really need to maintain a state
class TicketSeller
{
Ticket ticket,
Person salesPerson,
etc....
}
So I can do something like
if (salesPerson.canSellThisTicket())
{
salesPerson.soldATicket();
}
So I'm wondering what the preferred way to do something like this in rust would be.
Thank you