Created
July 11, 2024 08:49
-
-
Save zRains/9a2766b8fffd6a4446be931693e00ec5 to your computer and use it in GitHub Desktop.
little quiz
This file contains hidden or 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
#![feature(const_trait_impl)] | |
fn extend_lifetime(x: &str) -> &'static str { | |
f::<S>(x) | |
} | |
const fn f<'a, T: Tr<'a> + ~const Drop>(x: &'a str) -> T::Ty { | |
// can call g with T: Drop bound, even though | |
// T: ~const Drop works for non-Drop-implementing types | |
g::<T>(x) | |
} | |
#[allow(drop_bounds)] | |
const fn g<T: Drop>(x: &str) -> <T as Tr<'_>>::Ty { | |
x | |
} | |
struct S; | |
trait Tr<'a> { | |
type Ty; | |
} | |
#[allow(drop_bounds)] | |
impl<'a, T: Drop> Tr<'a> for T { | |
type Ty = &'a str; | |
} | |
impl<'a> Tr<'a> for S { | |
type Ty = &'static str; | |
} | |
fn main() { | |
let x = "Hello World".to_owned(); | |
let s = extend_lifetime(&x); | |
drop(x); | |
println!("{s}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment