---
title: "Bin operations"
description: "Guide to performing atomic multi-bin operations in Aerospike using the Rust client's operate method."
---

# Bin operations

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

You can use the Rust client API to perform separate operations on multiple bins in a record in a single command. This feature allows the application to modify and read bins of a record in a single transaction (that is, perform an atomic modification that returns the result).

Some record operations include:

| Operation | Description | Conditions |
| --- | --- | --- |
| `write` | Write a value to a bin. |  |
| `get` | Read the value of a bin. |  |
| `get_header` | Read only the metadata (generation and time-to-live) of the record. |  |
| `add` | Add an integer to the existing value of the bin. | The existing value must be integer. |
| `append` | Append a string to the existing value of the bin. | The existing value must be string. |
| `prepend` | Prepend a string to the existing value of the bin. | The existing value must be string. |
| `touch` | Rewrite the same record. | Generation and time-to-live are updated. |

::: note
Operations are performed in user-defined order.
:::

Both `Client::put` and `Client::operate` take a `WritePolicy` reference. There is no separate `OperatePolicy` type. Use `WritePolicy::default()` (or a configured `WritePolicy`) for atomic bin operations.

## Operation specification

To specify operations on different bins in the same transaction, create the bins with the values to apply:

```rust
let policy = WritePolicy::default();

let key = as_key!("test", "demoset", "opkey");

let bin1 = as_bin!("optintbin", 7);

let bin2 = as_bin!("optstringbin", "string value");

match client.put(&policy, &key, &[bin1, bin2]).await {

    Ok(()) => println!("Record written"),

    Err(err) => println!("Error writing record: {}", err),

};

let bin3 = as_bin!(bin1.name, 4);

let bin4 = as_bin!(bin2.name, "new string");
```

> 📖 **API reference**: [`Client::put`](https://docs.rs/aerospike/latest/aerospike/struct.Client.html#method.put)

Create the appropriate operations using the bins and supply them to the `operate()` function:

```rust
let ops = vec![

    operations::add(&bin3),

    operations::put(&bin4),

    operations::get(),

];

match client.operate(&policy, &key, &ops).await {

    Ok(record) => {

        if let Some(val) = record.bins.get(bin1.name.as_str()) {

            println!("optintbin: {}", val);

        } else {

            println!("bin {} not returned", bin1.name);

        }

    }

    Err(err) => println!("Error operating on record: {}", err),

}
```

> 📖 **API reference**: [`Client::operate`](https://docs.rs/aerospike/latest/aerospike/struct.Client.html#method.operate) | [`operations::add`](https://docs.rs/aerospike/latest/aerospike/operations/fn.add.html) | [`operations::put`](https://docs.rs/aerospike/latest/aerospike/operations/fn.put.html) | [`operations::get`](https://docs.rs/aerospike/latest/aerospike/operations/fn.get.html)

::: note
`record.bins` is a `HashMap` of bin names to values. Use `HashMap::get` (returns `Option`) when reading individual bins after an `operate` call. To print every bin returned by `operations::get()`, iterate directly: `for (name, value) in &record.bins { ... }`.
:::

## String append

To atomically append text to a string bin without reading the record first, pass `operations::append` to `operate` with a `WritePolicy`:

```rust
let key = as_key!("test", "posts", "post:1");

let ops = vec![operations::append(&as_bin!("body", " (edited)"))];

match client.operate(&WritePolicy::default(), &key, &ops).await {

    Ok(_record) => println!("Appended to body bin"),

    Err(err) => println!("Error operating on record: {}", err),

}
```

> 📖 **API reference**: [`operations::append`](https://docs.rs/aerospike/latest/aerospike/operations/fn.append.html) | [`Client::operate`](https://docs.rs/aerospike/latest/aerospike/struct.Client.html#method.operate) | [`WritePolicy::default`](https://docs.rs/aerospike/latest/aerospike/struct.WritePolicy.html)