{{#title std::unique_ptr — Rust ♡ C++}}
# std::unique\_ptr\

The Rust binding of std::unique\_ptr\ is called **[`UniquePtr`]**. See
the link for documentation of the Rust API.

[`UniquePtr`]: https://docs.rs/cxx/*/cxx/struct.UniquePtr.html

### Restrictions:

Only `std::unique_ptr>` is currently supported. Custom
deleters may be supported in the future.

UniquePtr\ does not support T being an opaque Rust type. You should use a
Box\ (C++ [rust::Box\](box.md)) instead for transferring ownership of
opaque Rust types on the language boundary.

## Example

UniquePtr is commonly useful for returning opaque C++ objects to Rust. This use
case was featured in the [*blobstore tutorial*](../tutorial.md).

```rust,noplayground
// src/main.rs

#[cxx::bridge]
mod ffi {
    unsafe extern "C++" {
        include!("example/include/blobstore.h");

        type BlobstoreClient;

        fn new_blobstore_client() -> UniquePtr;
        // ...
    }
}

fn main() {
    let client = ffi::new_blobstore_client();
    // ...
}
```

```cpp
// include/blobstore.h

#pragma once
#include 

class BlobstoreClient;

std::unique_ptr new_blobstore_client();
```

```cpp
// src/blobstore.cc

#include "example/include/blobstore.h"

std::unique_ptr new_blobstore_client() {
  return std::make_unique();
}
```