The Rust binding of std::unique_ptr<T> is called UniquePtr<T>. See the link for documentation of the Rust API.


Only std::unique_ptr<T, std::default_delete<T>> is currently supported. Custom deleters may be supported in the future.

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


UniquePtr is commonly useful for returning opaque C++ objects to Rust. This use case was featured in the blobstore tutorial.

// src/

mod ffi {
    unsafe extern "C++" {

        type BlobstoreClient;

        fn new_blobstore_client() -> UniquePtr<BlobstoreClient>;
        // ...

fn main() {
    let client = ffi::new_blobstore_client();
    // ...
// include/blobstore.h

#pragma once
#include <memory>

class BlobstoreClient;

std::unique_ptr<BlobstoreClient> new_blobstore_client();
// src/

#include "example/include/blobstore.h"

std::unique_ptr<BlobstoreClient> new_blobstore_client() {
  return std::make_unique<BlobstoreClient>();