Created
October 24, 2022 16:54
-
-
Save ClarkeRemy/5ab7e9d26f2504e1b3ca6cd4e849fd1c to your computer and use it in GitHub Desktop.
Lifetimes Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let x1 = vec![1_u8]; | |
let r1 = &x1[..]; // 'l1 | |
let ret = { | |
let x2 = vec![2_u8]; | |
let r2 = &x2[..]; // 'l2 | |
let mut y2 = Holder(r2);// Holder<'l2> | |
swap(&mut y2, r1); // Holder<'l2> | |
y2 = Holder(r1); // Holder<'l1> | |
y2 | |
}; | |
drop(ret); | |
} | |
type Slice<T> = [T]; // DST | |
type RefSlice<'a,T> = &'a Slice<T>; | |
type Array<T, const Len : usize> = [T; Len]; | |
struct Holder<'a>(&'a [u8]); | |
fn swap<'a:'c, 'b:'a, 'c>(h: &mut Holder<'a>, k:&'b [u8])->&'c[u8] { | |
let tmp = h.0; | |
h.0=k; | |
tmp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment