{{#title std::vector — Rust ♡ C++}}
# std::vector\

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

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

### Restrictions:

Rust code can never obtain a CxxVector by value. Instead in Rust code we will
only ever look at a vector behind a reference or smart pointer, as in
&CxxVector\ or UniquePtr\\>.

CxxVector\ does not support T being an opaque Rust type. You should use a
Vec\ (C++ rust::Vec\) instead for collections of opaque Rust types on
the language boundary.

## Example

This program involves Rust code converting a `CxxVector` (i.e.
`std::vector`) into a Rust `Vec`.

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

#![no_main] // main defined in C++ by main.cc

use cxx::{CxxString, CxxVector};

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        fn f(vec: &CxxVector);
    }
}

fn f(vec: &CxxVector) {
    let vec: Vec = vec
        .iter()
        .map(|s| s.to_string_lossy().into_owned())
        .collect();
    g(&vec);
}

fn g(vec: &[String]) {
    println!("{:?}", vec);
}
```

```cpp
// src/main.cc

#include "example/src/main.rs.h"
#include 
#include 

int main() {
  std::vector vec{"fearless", "concurrency"};
  f(vec);
}
```