---
title: "Mojo 1.2 發布：加入 pattern matching、二十多個數學函數上 GPU"
url: https://sammy.hk/mojo-1-2-pattern-matching-gpu/
author: Ethan
published: 2026-10-27T14:47:00+08:00
modified: 2026-10-27T09:56:03+08:00
description: "Mojo 1.2 已發布。官方版本說明列出三項重點：pattern matching、更多 std.math 函數可在 GPU 上運行，以及大型套件編譯速度提升。 Pattern matching 官方指出，match 語句會把一個值（subject）與一系列 pattern（case）比較，並執行第一個符合的區塊；p…"
categories: [AI, "開放源碼"]
tags: [GPU, Modular, Mojo, pattern matching, "編譯器"]
source: https://sammy.hk
---
![](https://sammy.hk/wp-content/uploads/2026/10/mojo-1-2-og.jpg)Mojo 1.2 已發布。官方版本說明列出三項重點：**pattern matching**、**更多 `std.math` 函數可在 GPU 上運行**，以及**大型套件編譯速度提升**。

## Pattern matching

官方指出，`match` 語句會把一個值（subject）與一系列 pattern（case）比較，並執行第一個符合的區塊；pattern 可以是字面值、or-pattern（`case 100 | 200:`）、tuple、guard（`case limit if limit > 10:`）、解構元素等。名字在 pattern 中出現時，Mojo 會把它綁定為讀部份值的不可變引用；動態 match 亦支援 `var`/`ref` 綁定。官方範例：

`mojo match status_code:     case 200:         print("OK")     case 301 | 302:         print("Redirect")     case 404:         print("Not found")     case code if code >= 500:         print("Server error", code)     case _:         print("Unexpected status", status_code)`

同一組 pattern 亦可用於 `if`、`while` 語句以及解構賦值；另支援 `comptime match` 用於測試編譯期值，並把名字綁定為參數值。

## 數學函數上 GPU

官方指出，**二十多個 `std.math` 函數不再呼叫系統 libm**，CPU 與 GPU 現時共用同一套 Mojo 實作，令 `atan()`、`tan()`、`cbrt()`、`erfc()`、`lgamma()`、`log10()`、`remainder()`、`hypot()`、Bessel 函數等**首次可在加速器上運行**；每個函數都記錄其準確度與 subnormal 行為。

## 編譯速度

官方數字：

- `nn` 套件的編譯時間由約 **98 秒**降至約 **61 秒**
- 在 Linux 上，編譯器改用 jemalloc 作為配置器，令大型 GPU kernel 檔案的編譯**快約 10% 至 13%**，原因是減少了在 glibc arena 管理上耗用的系統時間

## 破壞性改動

官方指出，`StringSpan` 現在只提供不可變的位元組序列存取，這個版本移除了 `MutStringSpan`、`MutStringSlice` 以及 `StringSpan` 的 `mut` 參數；以往透過 span 修改字串資料的程式碼需要更新。

## 文件

官方同時更新文件：Mojo 手冊的控制流程頁與語句參考頁已滲透 `match`、`comptime match`、pattern、`if case`、`while case` 以及 list 解構；語言參考新增詞法結構頁；「給 Python 開發者的 Mojo 提示」指南改寫為憶苦不會直接沾用的 Python 慣習；另新增 `@inline` 裝飾器參考頁。

## 參考資料

\[1\] [Mojo 官方版本說明 — Mojo v1.2.0](https://mojolang.org/releases/v1.2.0/)
\[2\] [Mojo 官方網站](https://mojolang.org/)

### 相關文章：

1. [Jac 語言登場：一個語法同時編譯到 Python、瀏覽器與原生程式](https://sammy.hk/jac-python-browser-code/ "Jac 語言登場：一個語法同時編譯到 Python、瀏覽器與原生程式")
2. [Go 推出平台無關 SIMD 實驗：寫一次、跨架構加速](https://sammy.hk/go-%e6%8e%a8%e5%87%ba%e5%b9%b3%e5%8f%b0%e7%84%a1%e9%97%9c-simd-%e5%af%a6%e9%a9%97%ef%bc%9a%e5%af%ab%e4%b8%80%e6%ac%a1%e3%80%81%e8%b7%a8%e6%9e%b6%e6%a7%8b%e5%8a%a0%e9%80%9f/ "Go 推出平台無關 SIMD 實驗：寫一次、跨架構加速")
3. [NVIDIA 推出 CUDA Rust：兩條路線用 Rust 寫 GPU 核心](https://sammy.hk/nvidia-%e6%8e%a8%e5%87%ba-cuda-rust%ef%bc%9a%e5%85%a9%e6%a2%9d%e8%b7%af%e7%b7%9a%e7%94%a8-rust-%e5%af%ab-gpu-%e6%a0%b8%e5%bf%83/ "NVIDIA 推出 CUDA Rust：兩條路線用 Rust 寫 GPU 核心")
4. [NVIDIA 推出 Minerva 為 AI 工廠提供端到端觀測平台](https://sammy.hk/nvidia-launch-minerva-ai-observability-platform/ "NVIDIA 推出 Minerva 為 AI 工廠提供端到端觀測平台")
