# Struct alloc::arc::Arc [−] [src]

    pub struct Arc<T: ?Sized> {
        // some fields omitted
    }

1.0.0

An atomically reference counted wrapper for shared state.

# Examples

In this example, a large vector is shared between several threads. With simple pipes, without `Arc`, a copy would have to be made for each thread.

When you clone an `Arc<T>`, it will create another pointer to the data and increase the reference counter.

    use std::sync::Arc;
    use std::thread;

    fn main() {
        let numbers: Vec<_> = (0..100u32).collect();
        let shared_numbers = Arc::new(numbers);

        for _ in 0..10 {
            let child_numbers = shared_numbers.clone();

            thread::spawn(move || {
                let local_numbers = &child_numbers[..];

                // Work with the local numbers
            });
        }
    }

## Methods

### `impl<T> Arc<T>`

#### `fn new(data: T) -> Arc<T>`

Constructs a new `Arc<T>`.

# Examples

    use std::sync::Arc;

    let five = Arc::new(5);

#### 1.4.0 `fn try_unwrap(this: Self) -> Result<T, Self>`

Unwraps the contained value if the `Arc<T>` has exactly one strong reference.

Otherwise, an `Err` is returned with the same `Arc<T>`.

This will succeed even if there are outstanding weak references.

# Examples

    use std::sync::Arc;

    let x = Arc::new(3);
    assert_eq!(Arc::try_unwrap(x), Ok(3));

    let x = Arc::new(4);
    let _y = x.clone();
    assert_eq!(Arc::try_unwrap(x), Err(Arc::new(4)));

### `impl<T: ?Sized> Arc<T>`

#### 1.4.0 `fn downgrade(this: &Self) -> Weak<T>`

Downgrades the `Arc<T>` to a `Weak<T>` reference.

# Examples

    use std::sync::Arc;

    let five = Arc::new(5);

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

#### `fn weak_count(this: &Self) -> usize`

Unstable (`arc_counts` #28356)

: not clearly useful, and racy

Get the number of weak references to this value.

#### `fn strong_count(this: &Self) -> usize`

Unstable (`arc_counts` #28356)

: not clearly useful, and racy

Get the number of strong references to this value.

### `impl<T: Clone> Arc<T>`

#### 1.4.0 `fn make_mut(this: &mut Self) -> &mut T`

Make a mutable reference into the given `Arc<T>`. If the `Arc<T>` has more than one strong reference, or any weak references, the inner data is cloned.

This is also referred to as a copy-on-write.

# Examples

    use std::sync::Arc;

    let mut data = Arc::new(5);

    *Arc::make_mut(&mut data) += 1;         // Won't clone anything
    let mut other_data = data.clone();      // Won't clone inner data
    *Arc::make_mut(&mut data) += 1;         // Clones inner data
    *Arc::make_mut(&mut data) += 1;         // Won't clone anything
    *Arc::make_mut(&mut other_data) *= 2;   // Won't clone anything

    // Note: data and other_data now point to different numbers
    assert_eq!(*data, 8);
    assert_eq!(*other_data, 12);

### `impl<T: ?Sized> Arc<T>`

#### 1.4.0 `fn get_mut(this: &mut Self) -> Option<&mut T>`

Returns a mutable reference to the contained value if the `Arc<T>` has one strong reference and no weak references.

# Examples

    use std::sync::Arc;

    let mut x = Arc::new(3);
    *Arc::get_mut(&mut x).unwrap() = 4;
    assert_eq!(*x, 4);

    let _y = x.clone();
    assert!(Arc::get_mut(&mut x).is_none());

## Trait Implementations

### `impl<T: ?Sized + Sync + Send> Send for Arc<T>`

### `impl<T: ?Sized + Sync + Send> Sync for Arc<T>`

### `impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T>`

### `impl<T: ?Sized> Clone for Arc<T>`

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

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

### `impl<T: ?Sized> Deref for Arc<T>`

#### `type Target = T`

#### `fn deref(&self) -> &T`

### `impl<T: ?Sized> Drop for Arc<T>`

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

### `impl<T: ?Sized + PartialEq> PartialEq for Arc<T>`

#### `fn eq(&self, other: &Arc<T>) -> bool`

#### `fn ne(&self, other: &Arc<T>) -> bool`

### `impl<T: ?Sized + PartialOrd> PartialOrd for Arc<T>`

#### `fn partial_cmp(&self, other: &Arc<T>) -> Option<Ordering>`

#### `fn lt(&self, other: &Arc<T>) -> bool`

#### `fn le(&self, other: &Arc<T>) -> bool`

#### `fn gt(&self, other: &Arc<T>) -> bool`

#### `fn ge(&self, other: &Arc<T>) -> bool`

### `impl<T: ?Sized + Ord> Ord for Arc<T>`

#### `fn cmp(&self, other: &Arc<T>) -> Ordering`

### `impl<T: ?Sized + Eq> Eq for Arc<T>`

### `impl<T: ?Sized + Display> Display for Arc<T>`

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

### `impl<T: ?Sized + Debug> Debug for Arc<T>`

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

### `impl<T: ?Sized> Pointer for Arc<T>`

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

### `impl<T: Default> Default for Arc<T>`

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

### `impl<T: ?Sized + Hash> Hash for Arc<T>`

#### `fn hash<H: Hasher>(&self, state: &mut H)`

#### 1.3.0 `fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher`

### `impl<T> From<T> for Arc<T>`1.6.0

#### `fn from(t: T) -> Self`

### `impl<T: ?Sized> Borrow<T> for Arc<T>`

#### `fn borrow(&self) -> &T`

### `impl<T: ?Sized> AsRef<T> for Arc<T>`1.5.0

#### `fn as_ref(&self) -> &T`

# 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

## 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`)