---
title: "Apply a UDF"
description: "Learn how to invoke Record User Defined Functions (UDFs) on single records using the Aerospike Rust client's execute_..."
---

# Apply a UDF

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

## Single record

UDFs that execute on a single record are Record UDFs. The record may or may not exist in the database, which allows the UDF to update or create a record.

To invoke a Record UDF, use `Client.execute_udf()`:

```rust
pub async fn execute_udf(

        &self,

        policy: &WritePolicy,

        key: &Key,

        server_path: &str,

        function_name: &str,

        args: Option<&[Value]>,

    ) -> Result<Option<Value>>
```

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

Where,

-   `policy`: The policy governing the operation.
-   `key`: The key of the record on which to invoke the function.
-   `server_path`: The name of the package containing the named function, without the `.lua` extension.
-   `udf_name`: The UDF module that contains the function to invoke.
-   `function_name`: The function to invoke.
-   `args`: The optional function arguments.

This example defines a UDF in the module `examples.lua`:

```lua
function readBin(r, name)

    return r[name]

end
```

`readBin` returns the value of record `r` in bin `name`.

The client application can invoke `readBin` on a record:

```rust
let result = client.execute_udf(&WritePolicy::default(),

    &key, "examples", "readBin", Some(&[as_val!("name")]))

    .await;
```

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

`key` specifies the record to pass to the UDF as the parameter `r`.

## Multiple arguments

If the UDF accepts multiple arguments, add each argument to `client.execute_udf()`.

For example, if the following UDF is defined in _example.lua_:

```lua
function multiplyAndAdd(r, a, b)

    return r['factor'] * a + b;

end
```

This multiplies the bin `factor` by `a` and adds `b`, and returns results to the caller. Then, to invoke `multiplyAndAdd()` from Rust, run:

```rust
let result = client.execute_udf(&WritePolicy::default(),

    &key, "examples", "multiplyAndAdd", Some(&[as_val!(10), as_val!(5)]))

    .await;
```

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