These are chat archives for rust-lang/rust

12th
Jan 2019
Matteo Stefanini
@mstefanini_gitlab
Jan 12 11:51

Hi all!
I've a newbie question about the lifetime parameter.

I'm trying to define a struct that it owns another struct with a lifetime parameter specified and i received the error: "error[E0106]: missing lifetime specifier". I'm not able to cope it, in this case if the struct owns it why i need a lifetime specifier?

Michal 'vorner' Vaner
@vorner
Jan 12 12:00
The struct still transitively borrows something. If that something goes away, your struct can no longer be used. That's what the lifetime means.
Matteo Stefanini
@mstefanini_gitlab
Jan 12 12:12
Oh, you're right! So i think that i need to implement a lifetime parameter on my struct and pass it to the struct that i own.
Genysys
@Genysys
Jan 12 16:49

learning rust and cant understand why the output of my println! is empty.

#[derive(Debug)]
struct Coords {
    pub x: i64,
    pub y: i64,
}

fn shift_x_twice(coords: &mut Coords, delta: &i64) {
    coords.x += *delta;
    coords.x += *delta;
}

fn main() {
    //println!("Hello, world!");

    let mut a = Coords{x:10, y:10};
    let delta_a = 10;
    // b =  shift_x_twice(&mut a , &delta_a)
    //  println!("Here is the result : {:?}", b);
    println!("Here is the result : {:?}", shift_x_twice(&mut a , &delta_a));

}

Result:

 cargo run
   Compiling under_lifetimes v0.1.0 (/Users/samueldare/Documents/Code/Rust/under_lifetimes)
    Finished dev [unoptimized + debuginfo] target(s) in 0.43s
     Running `target/debug/under_lifetimes`
Here is the result : ()
Michal 'vorner' Vaner
@vorner
Jan 12 16:50
Because that shift_x_twice doesn't return anything.
It just modifies the parameter in place
Genysys
@Genysys
Jan 12 16:52
sorry if its a silly question
but how do I return the value for shift_twice?
add a return on the shift_value function?
Michal 'vorner' Vaner
@vorner
Jan 12 17:10
These are kind of basics. Not that people wouldn't be willing to help, but generally there are good tutorials and beginner introductions out there. Are you following some of them? Maybe the Rust Book?
thojest
@thojest
Jan 12 17:11
@Genysys leave out the semicolon
Genysys
@Genysys
Jan 12 17:48
thanks! @thojest / @vorner