std::sync::Weak - Rust
Description: API documentation for the Rust `Weak` struct in crate `std`.
Keywords: rust, rustlang, rust-lang, Weak

# Struct std::sync::Weak 1.4.0 [−] [src]

    pub struct Weak<T> where T: ?Sized { /* fields omitted */ }

A weak version of `Arc`.

`Weak` pointers do not count towards determining if the inner value should be dropped.

The typical way to obtain a `Weak` pointer is to call `Arc::downgrade`.

See the `Arc` documentation for more details.

## Methods

### `impl<T> Weak<T>`
[src]

#### `fn new() -> Weak<T>`
1.10.0

Constructs a new `Weak<T>`, without an accompanying instance of `T`.

This allocates memory for `T`, but does not initialize it. Calling `upgrade` on the return value always gives `None`.

# Examples

    use std::sync::Weak;

    let empty: Weak<i64> = Weak::new();
    assert!(empty.upgrade().is_none());Run

### `impl<T> Weak<T> where T: ?Sized`
[src]

#### `fn upgrade(&self) -> Option<Arc<T>>`

Upgrades the `Weak` pointer to an `Arc`, if possible.

Returns `None` if the strong count has reached zero and the inner value was destroyed.

# Examples

    use std::sync::Arc;

    let five = Arc::new(5);

    let weak_five = Arc::downgrade(&five);

    let strong_five: Option<Arc<_>> = weak_five.upgrade();
    assert!(strong_five.is_some());

    // Destroy all strong pointers.
    drop(strong_five);
    drop(five);

    assert!(weak_five.upgrade().is_none());Run

## Trait Implementations

### `impl<T> Send for Weak<T> where T: Send + Sync + ?Sized`
[src]

### `impl<T> Clone for Weak<T> where T: ?Sized`
[src]

#### `fn clone(&self) -> Weak<T>`

Makes a clone of the `Weak` pointer.

This creates another pointer to the same inner value, increasing the weak reference count.

# Examples

    use std::sync::Arc;

    let weak_five = Arc::downgrade(&Arc::new(5));

    weak_five.clone();Run

#### `fn clone_from(&mut self, source: &Self)`
1.0.0

Performs copy-assignment from `source`. Read more

### `impl<T, U> CoerceUnsized<Weak<U>> for Weak<T> where T: Unsize<U> + ?Sized,
U: ?Sized`
[src]

### `impl<T> Debug for Weak<T> where T: Debug + ?Sized`
[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result<(), Error>`

Formats the value using the given formatter.

### `impl<T> Default for Weak<T>`
1.10.0
[src]

#### `fn default() -> Weak<T>`

Constructs a new `Weak<T>`, without an accompanying instance of `T`.

This allocates memory for `T`, but does not initialize it. Calling `upgrade` on the return value always gives `None`.

# Examples

    use std::sync::Weak;

    let empty: Weak<i64> = Default::default();
    assert!(empty.upgrade().is_none());Run

### `impl<T> Drop for Weak<T> where T: ?Sized`
[src]

#### `fn drop(&mut self)`

Drops the `Weak` pointer.

This will decrement the weak reference count.

# Examples

    use std::sync::Arc;

    struct Foo;

    impl Drop for Foo {
        fn drop(&mut self) {
            println!("dropped!");
        }
    }

    let foo = Arc::new(Foo);
    let weak_foo = Arc::downgrade(&foo);
    let other_weak_foo = weak_foo.clone();

    drop(weak_foo);   // Doesn't print anything
    drop(foo);        // Prints "dropped!"

    assert!(other_weak_foo.upgrade().is_none());Run

### `impl<T> Sync for Weak<T> where T: Send + Sync + ?Sized`
[src]

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)