[syntax game] Taking Ownership of a String Multiple Times?

⚓ Rust    📅 2025-09-20    👤 surdeus    👁️ 8      

surdeus

Warning

This post was published 42 days ago. The information described in this article may have changed.

How to make the following code to pass the compilation? :face_with_raised_eyebrow:

Rules:

  1. You cannot modify the function, you are only allowed to add code surrounding it.
  2. You can use macros, but when all macros expand the code must exist, which means it's not allowed to use #[cfg(...)] or stringify! to hide that function.
  3. You cannot put it inside comments (/* */) or string literals (" ").
  4. There is at least one workaround exists, maybe more.
  5. Must be able to compile in Playground.
fn foo() -> String {
    let a: String = "a".to_string();
    let b = a;
    let c = a;
    a
}

5 posts - 4 participants

Read full topic

🏷️ Rust_feed