Search code examples
rustiteratortuplesunzip

Rust unzip() type annotations needed


I'm trying to unzip nested tuples and use a combinator to do that:

let create_task = |msg| {
    // We need to keep the message_id to report failures to SQS
    // and the receipt_handle to delete the processed messages from SQS.
    let SqsMessageObj {
        message_id,
        body,
        receipt_handle,
        ..
    } = msg;

    let span = tracing::span!(tracing::Level::INFO, "Handling SQS msg", message_id);
    let task = async {
        let item = serde_json::from_value(body)?;
        f(item, app_config).await
    };

    (
        (
            message_id.unwrap_or_default(),
            receipt_handle.unwrap_or_default(),
        ),
        task,
    )
};

print!("{:?}", event.payload);

let ((ids, receipts), tasks): ((_, _), _) = event
    .payload
    .records
    .into_iter()
    .map(create_task)
    .unzip::<(_, _), _, (_, _), _>();

The compiler isn't satisfied with it. What is the unzip() type annotations should be?


Solution

  • This is done by using Vec<_> type for all the tuple elements:

        let ((ids, receipts), tasks): ((Vec<_>, Vec<_>), Vec<_>) = event.payload.records.into_iter().map(create_task).unzip();