Hi! I came from C#/Kotlin/Java/ES and such. Really like Rust, but some things I yet to understand. Guess I need to make my brains work a little different than I used to for all those years
I have a simple code here that compiles and runs
struct Controller<'a> {
environment: &'a Environment<'a>,
something: &'static str
}
impl<'a> Controller<'a> {
fn new(env: &'a Environment) -> Self{
Controller{
environment: env,
something: "abcdef"
}
}
fn do_smt(&self){
println!("from controller itself: {}", self.something)
}
}
struct Environment<'a> {
wind_speed: u64,
game_time: &'a mut GameTime
}
impl<'a> Environment<'a> {
fn new(wind_speed: u64, game_time: &'a mut GameTime) -> Self{
Environment{
wind_speed,
game_time
}
}
fn change_wind_speed(&mut self, value: u64){
self.wind_speed = value
}
fn set_game_time(&mut self, new_time: &'a mut GameTime){
self.game_time = new_time
}
fn do_smt(&self){
println!("from env itself: {}", self.wind_speed)
}
}
struct GameTime {
day: u64
}
impl GameTime {
fn new(day: u64) -> Self{
GameTime{
day
}
}
fn change_day(&mut self, value: u64){
self.day = value
}
}
fn main() {
let mut gt = GameTime::new(3);
let mut env = Environment::new(6, &mut gt);
let c = Controller::new(&env);
let newgt = &mut GameTime::new(222);
println!("{0}, {1}", env.wind_speed, env.game_time.day);
env.change_wind_speed(5);
env.game_time.change_day(10);
println!("{0}, {1}", env.wind_speed, env.game_time.day);
env.set_game_time(newgt);
println!("{0}, {1}", env.wind_speed, env.game_time.day);
//c.do_smt();
env.do_smt();
}
... that's until I uncomment the "c.do_smt();" line at the end. Why can't I call a method that does not mutate anything on that object? Even thou exactly same "env.do_smt();" does what I expect it to do?
Sorry for noob questions, but I really tried do google something out)