Skip to main content

take

Function take 

Source
pub async fn take<T: DeserializeOwned>(
    session: &Session,
    key: &str,
) -> Result<Option<T>, Error>
Expand description

Reads and clears a “flash” value - like session.remove(key), but only actually calls it when key was present. Prefer this for a flash- message read that runs on every page view whether or not one was queued (the common Laravel-style “check for a flashed success/error message on every response” pattern - see demo/app/Http/Controllers/ post_controller.rs’s index for a real example).

The difference matters: tower_sessions::Session::remove marks the session modified unconditionally - confirmed by reading tower-sessions-core’s own remove_value, which flips its internal is_modified flag before even checking whether the underlying map had the key at all. A flash-message check is exactly the case where the key is usually absent (most page views have nothing flashed), so a bare .remove() in a handler that runs on every request writes to the session store on every single request, not just the ones that actually redirected in with a message. Under enough traffic this is a real, reproduced production issue, not a theoretical one - see docs/GOTCHAS.md.