pub fn attach(self) -> ContextGuard {
        let previous_cx = CURRENT_CONTEXT
            .try_with(|current| current.replace(self))
            .ok();

        ContextGuard {
            previous_cx,
            _marker: PhantomData,
        }
    }
/// Replaces the current context on this thread with this context.
///
/// Dropping the returned [`ContextGuard`] will reset the current context to the
/// previous value.
///
///
/// # Examples
///
/// ```
/// use opentelemetry::Context;
///
/// #[derive(Debug, PartialEq)]
/// struct ValueA(&'static str);
///
/// let my_cx = Context::new().with_value(ValueA("a"));
///
/// // Set the current thread context
/// let cx_guard = my_cx.attach();
/// assert_eq!(Context::current().get::<ValueA>(), Some(&ValueA("a")));
///
/// // Drop the guard to restore the previous context
/// drop(cx_guard);
/// assert_eq!(Context::current().get::<ValueA>(), None);
/// ```
///
/// Guards do not need to be explicitly dropped:
///
/// ```
/// use opentelemetry::Context;
///
/// #[derive(Debug, PartialEq)]
/// struct ValueA(&'static str);
///
/// fn my_function() -> String {
///     // attach a context the duration of this function.
///     let my_cx = Context::new().with_value(ValueA("a"));
///     // NOTE: a variable name after the underscore is **required** or rust
///     // will drop the guard, restoring the previous context _immediately_.
///     let _guard = my_cx.attach();
///
///     // anything happening in functions we call can still access my_cx...
///     my_other_function();
///
///     // returning from the function drops the guard, exiting the span.
///     return "Hello world".to_owned();
/// }
///
/// fn my_other_function() {
///     // ...
/// }
/// ```
/// Sub-scopes may be created to limit the duration for which the span is
/// entered:
///
/// ```
/// use opentelemetry::Context;
///
/// #[derive(Debug, PartialEq)]
/// struct ValueA(&'static str);
///
/// let my_cx = Context::new().with_value(ValueA("a"));
///
/// {
///     let _guard = my_cx.attach();
///
///     // the current context can access variables in
///     assert_eq!(Context::current().get::<ValueA>(), Some(&ValueA("a")));
///
///     // exiting the scope drops the guard, detaching the context.
/// }
///
/// // this is back in the default empty context
/// assert_eq!(Context::current().get::<ValueA>(), None);
/// ```
Replaces the current context on this thread with this context.
Dropping the returned [`ContextGuard`] will reset the current context to the
previous value.
Replaces the current context on this thread with this context . Dropping the returned [ ` ContextGuard ` ] will reset the current context to the previous value .
pub fn attach(self) -> ContextGuard {
        let previous_cx = CURRENT_CONTEXT
            .try_with(|current| current.replace(self))
            .ok();
        ContextGuard {
            previous_cx,
            _marker: PhantomData,
        }
    }
pub fn attach ( self ) -> ContextGuard { let previous_cx = CURRENT_CONTEXT . try_with ( | current | current . replace ( self ) ) . ok ( ) ; ContextGuard { previous_cx , _marker : PhantomData , } }
Replaces the current context on this thread with this context.
Replaces the current context on this thread with this context .
self
self
examples
```
use opentelemetry::Context.
` ` ` use opentelemetry :: Context .
[derive(debug, partialeq)]
struct ValueA(&'static str).

let my_cx = Context::new().with_value(ValueA("a")).

Set the current thread context
let cx_guard = my_cx.attach();
assert_eq!(Context::current().get::(), Some(&ValueA("a"))).

Drop the guard to restore the previous context
drop(cx_guard);
assert_eq!(Context::current().get::(), None);
```

Guards do not need to be explicitly dropped.

```
use opentelemetry::Context.
struct ValueA ( & ' static str ) . let my_cx = Context :: new () . with_value ( ValueA ( " a " )) . Set the current thread context let cx_guard = my_cx . attach () ; assert_eq! ( Context :: current () . get :: () Some ( &ValueA ( " a " ))) . Drop the guard to restore the previous context drop ( cx_guard ) ; assert_eq! ( Context :: current () . get :: () None ) ; ` ` ` Guards do not need to be explicitly dropped . ` ` ` use opentelemetry :: Context .
[derive(debug, partialeq)]
struct ValueA(&'static str).

fn my_function() -> String {
attach a context the duration of this function.
let my_cx = Context::new().with_value(ValueA("a"));
NOTE: a variable name after the underscore is **required** or rust
will drop the guard, restoring the previous context _immediately_.
let _guard = my_cx.attach().

anything happening in functions we call can still access my_cx
my_other_function().

returning from the function drops the guard, exiting the span.
return "Hello world".to_owned();
}

fn my_other_function() {

}
```
Sub-scopes may be created to limit the duration for which the span is
entered.

```
use opentelemetry::Context.
struct ValueA ( & ' static str ) . fn my_function () - > String { attach a context the duration of this function . let my_cx = Context :: new () . with_value ( ValueA ( " a " )) ; NOTE : a variable name after the underscore is ** required ** or rust will drop the guard restoring the previous context _immediately_ . let _guard = my_cx . attach () . anything happening in functions we call can still access my_cx my_other_function () . returning from the function drops the guard exiting the span . return " Hello world " . to_owned () ; } fn my_other_function () { } ` ` ` Sub - scopes may be created to limit the duration for which the span is entered . ` ` ` use opentelemetry :: Context .
[derive(debug, partialeq)]
struct ValueA(&'static str).

let my_cx = Context::new().with_value(ValueA("a")).

{
let _guard = my_cx.attach().

the current context can access variables in
assert_eq!(Context::current().get::(), Some(&ValueA("a"))).

exiting the scope drops the guard, detaching the context.

this is back in the default empty context
assert_eq!(Context::current().get::(), None);
```
struct ValueA ( & ' static str ) . let my_cx = Context :: new () . with_value ( ValueA ( " a " )) . { let _guard = my_cx . attach () . the current context can access variables in assert_eq! ( Context :: current () . get :: () Some ( &ValueA ( " a " ))) . exiting the scope drops the guard detaching the context . this is back in the default empty context assert_eq! ( Context :: current () . get :: () None ) ; ` ` `