Mutable struct in function trait

Hi,

I am new to Rust and I just got stuck on this problem: I want to write functions that are operating on a struct and changing the state of the struct. The following code works, however, I started by writing what is now commented.

pub trait Model {
    fn perform_calculations(&mut self);
    fn get_results(& self) -> Vec<f64>;
}

pub fn func1<T: Model>(model: &mut T) {
    model.perform_calculations();
}

pub fn func2<T: Model>(model: &mut T) {
    func1(model);
    //func1(&mut model);

    let results = model.get_results();
   // perform calculations with the results
}

Hence I was wondering, is the model copied when the call to func1 is made in func2 or is it what I intended : operating on the model which is called consecutively across all the functions. Is the call on get_results the intended one (with the state of the model being update after the call to func1) ?

Thanks for your consideration.

P-S: I have decided to use a struct being able to perform the calculations and holding the results rather then a function as I want to use the results in differents places across my code without having to recompute the model. I don't know if it is the best choice.

A mutable reference will only borrow it. In fact, func2 is not allowed to clone it at all, as it doesn't know whether T implements Clone.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.