# Make \`#\[may\_dangle\]\` more functional

**URL:** <https://internals.rust-lang.org/t/make-may-dangle-more-functional/18705>\
**Category:** language design\
**Created:** [April 19, 2023, 3:35am UTC](https://internals.rust-lang.org/t/make-may-dangle-more-functional/18705 "2023-04-19T03:35:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qsh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qsh/32/10418_2.png) [@Qsh](https://internals.rust-lang.org/u/Qsh)\
**Post date:** [April 19, 2023, 3:35am UTC](https://internals.rust-lang.org/t/make-may-dangle-more-functional/18705/1 "2023-04-19T03:35:49Z")

</div>

## 1. Allow `#[may_dangle]` to appear before the generic lifetime of the structure generates a declaration.

```rust
struct Foo<#[may_dangle] 's> {...}

```

If this is done, the dangle of the lifetime `'s` will not make the type `Foo<'s>` also become dangle.

If this is done, then the use of the lifetime `'s` within the structure `Foo` can only be done using the method of `#[may_dangle]`.

```rust
struct Bar<#[may_dangle] 's> {...}
struct Foo<#[may_dangle] 's> {
    bar: Bar<'s>,
}

```

Normal use will be rejected by the compiler:

```rust
struct Foo<#[may_dangle] 's> {
    bar: &'s i32,
// ^^^^^^^ &'s i32 not allow 's dangle
}

```

The survival time of a structure is independent of its lifetime `'s`.

```rust
Foo<'s>: 'static

```

## 2. Allow `#[may_dangle]` to modify any type

Make `#[may_dangle] &'s i32` a legal type:

```rust
fn foo<#[may_dangle] 's>(x: #[may_dangle] &'s i32) {...}

```

Values with type `#[may_dangle] T` can only be copied (if `T` can be copied) and moved, and cannot read members or call methods.

Therefore, `x` cannot be dereferenced.

Types with `#[may_dangle]` modification can also serve as member types of structural entities.

```rust
struct Foo<#[may_dangle] 's> {
    bar: #[may_dangle] &'s i32,
}

```

The survival time of `#[may_dangle] T` is `'static`

```rust
#[may_dangle] T: 'static

```

## 3. Using a value of `#[may_dangle]` will not result in an extension of the lifetime

```rust
fn foo<#[may_dangle] 's>(x: #[may_dangle] &'s i32) {...}

let a: #[may_dangle] &i32;
{
    let x = 1;
    a = &x;
}
foo(a);

```

* * *

## These are part of the specific design and implementation of

> [@Idea: Discontinuous lifetime and its borrowing](https://internals.rust-lang.org/t/idea-discontinuous-lifetime-and-its-borrowing/18699):
>
> Currently, the lifecycle is continuous, and borrowing using the lifecycle must not survive beyond its lifespan (ignore dropck\_eyepatch). If we want to store a mutable pointer at the same time, we have to use RefCell or UnsafeCell to do this. Now, propose a new lifetime form that will allow for discontinuous availability of its resources. The method is to add a keyword with a tentative name of discontinuity, which will cause the following code to compile let mut x = 1; let a = #[discontinuity…

* * *

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [July 18, 2023, 3:36am UTC](https://internals.rust-lang.org/t/make-may-dangle-more-functional/18705/2 "2023-07-18T03:36:22Z")

</div>

Closed on July 18, 2023, 3:36am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
