---
title: "swap(_:_:)"
framework: swift
role: symbol
role_heading: Function
path: "swift/swap(_:_:)"
---

# swap(_:_:)

Exchanges the values of the two arguments.

## Declaration

```swift
func swap<T>(_ a: inout T, _ b: inout T) where T : ~Copyable
```

## Parameters

- `a`: The first value to swap.
- `b`: The second value to swap.

## Discussion

Discussion The two arguments must not alias each other. To swap two elements of a mutable collection, use the swapAt(_:_:) method of that collection instead of this function.

## See Also

### Memory Access

- [withUnsafePointer(to:_:)](swift/withunsafepointer(to:_:)-9fjn6.md)
- [withUnsafePointer(to:_:)](swift/withunsafepointer(to:_:)-35wrn.md)
- [withUnsafeMutablePointer(to:_:)](swift/withunsafemutablepointer(to:_:).md)
- [withUnsafeBytes(of:_:)](swift/withunsafebytes(of:_:)-5zxtl.md)
- [withUnsafeBytes(of:_:)](swift/withunsafebytes(of:_:)-5gesg.md)
- [withUnsafeMutableBytes(of:_:)](swift/withunsafemutablebytes(of:_:).md)
- [withUnsafeTemporaryAllocation(of:capacity:_:)](swift/withunsafetemporaryallocation(of:capacity:_:).md)
- [withUnsafeTemporaryAllocation(byteCount:alignment:_:)](swift/withunsafetemporaryallocation(bytecount:alignment:_:).md)
- [exchange(_:with:)](swift/exchange(_:with:).md)
