"""Converts a given rotation matrix to a quaternion.

The function uses the `Rotation` class from `scipy.spatial.transform` to perform the conversion. The input rotation matrix can have any number of leading dimensions, but the last two dimensions must be (3, 3). The output quaternion is represented in the (x, y, z, w) format.

Args:
    rmat (np.array): A rotation matrix with shape (..., 3, 3).

Returns:
    np.array: A quaternion with shape (..., 4) in the (x, y, z, w) format.
"""
You should write code starting with:
from scipy.spatial.transform import Rotation as R
import numpy as np

def mat2quat(rmat: np.ndarray) -> np.ndarray:
    # TODO: Implement this function
    pass

