#Variance

26 messages · Page 1 of 1 (latest)

visual crown
#
fn debug<'a>(a: &'a str, b: &'a str) {
    println!("a = {a:?} b = {b:?}");
}

fn main() {
    let hello: &'static str = "hello";
    {
        let world = String::from("world");
        let world = &world; // 'world has a shorter lifetime than 'static
        debug(hello, world); // hello silently downgrades from `&'static str` into `&'world str`
    }
  println!("{}",hello);
}

If the lifetime of hello is downgraded then why the last print line is working.??

#

Or it is not really downgrading insted something like the intersection of lifetime of 'world and 'static = 'a

stiff tangle
visual crown
#

Oh

stiff tangle
#

it pretty much makes a new reference to pass into debug

stiff tangle
#

it's debug(&*hello, &*world)

visual crown
#

Internally.??

stiff tangle
#

pretty much

#

you can do it yourself

visual crown
#

Got it

stiff tangle
#

it's sometimes required for mutable references, because they are moved as you cannot copy them

#

but it's rare

visual crown
#

So 'a = 'world

#

As 'static will cover the 'world

#

Something like this

#

As 'static covers the 'world part so we took 'a world as the common reigion

#

For me it's the meaning of downgrading in this context

stiff tangle
# visual crown

no, in this case 'a is even shorter, it just exists during the debug function call, it's not 'world

visual crown
#

Yeah I am getting it

stiff tangle
#

'a is not really a concrete lifetime at all, it's just one that fits the constraints

visual crown
#

Is this what you wana say. (ignore 'a = 'world)