# How in the Rust bindings do I know an LWT failed?

**URL:** <https://forum.scylladb.com/t/how-in-the-rust-bindings-do-i-know-an-lwt-failed/1007>\
**Category:** ScyllaDB\
**Created:** [November 15, 2023, 3:06am UTC](https://forum.scylladb.com/t/how-in-the-rust-bindings-do-i-know-an-lwt-failed/1007 "2023-11-15T03:06:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chase\_Lewis](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.scylladb.com/chase_lewis/32/520_2.png) [@Chase\_Lewis](https://forum.scylladb.com/u/Chase_Lewis)\
**Post date:** [November 15, 2023, 3:06am UTC](https://forum.scylladb.com/t/how-in-the-rust-bindings-do-i-know-an-lwt-failed/1007/1 "2023-11-15T03:06:16Z")

</div>

Getting my feet wet with a small app with scylladb. I have a unique key I don’t want duplicated on insert so I have an LWT like below. I’m curious how I can know the LWT failed so I can return back a true/false so I can alert the client appropriately that the username is taken in case a race condition bypasses the basic client check.

```auto
pub async fn create_user(session: Session, user: User) -> anyhow::Result<()> {

    session.query(r#"
        INSERT INTO labeling.users
        (username,password_hash,first_name,last_name,created_at) 
        VALUES
        (?,?,?,?,?)
        IF NOT EXISTS;
    "#,user).await?;

    return Ok(());
}

```

---

<div class="post-metadata">

**Author:** ![Chase\_Lewis](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.scylladb.com/chase_lewis/32/520_2.png) [@Chase\_Lewis](https://forum.scylladb.com/u/Chase_Lewis)\
**Post date:** [November 15, 2023, 6:56pm UTC](https://forum.scylladb.com/t/how-in-the-rust-bindings-do-i-know-an-lwt-failed/1007/2 "2023-11-15T18:56:00Z")

</div>

```auto
pub async fn create_user(session: &Session, user: &User) -> anyhow::Result<bool> {

   let row = session.query(r#"
        INSERT INTO labeling.users
        (username,password_hash,email,first_name,last_name,created_at) 
        VALUES
        (?,?,?,?,?,?)
        IF NOT EXISTS;
    "#,(
        user.username.clone(),
        user.password_hash.clone(),
        user.email.clone(),
        user.first_name.clone(),
        user.last_name.clone(),
        user.created_at
    )).await?.first_row()?;

    if let Some(Some(applied)) = row.columns.get(0) {
        return Ok(applied.as_boolean().unwrap());
    }

    return Ok(false);
}

```

So the first row of any insert with ‘IF NOT EXISTS’ is always a boolean with the name ‘applied’. However, what makes this complicated is if the row DOES exist it will also return the existing row. This means the output format changes based on if it succeeds or not …

The code above was the best way I could find to handle that.

---

<div class="post-metadata">

**Author:** ![Konstantin\_Osipov](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.scylladb.com/konstantin_osipov/32/301_2.png) [@Konstantin\_Osipov](https://forum.scylladb.com/u/Konstantin_Osipov)\
**Post date:** [November 27, 2023, 1:26pm UTC](https://forum.scylladb.com/t/how-in-the-rust-bindings-do-i-know-an-lwt-failed/1007/3 "2023-11-27T13:26:00Z")

</div>

You must be playing with Cassandra, in Scylla the statement metadata is stable: [How does Scylla LWT Differ from Apache Cassandra ? | ScyllaDB Docs](https://opensource.docs.scylladb.com/stable/kb/lwt-differences.html)
