コンテンツにスキップ
PR

Go と Rust の違い

Goは運用しやすいサーバー・CLI、Rustは安全性と性能が必要なシステム寄りの開発に向きます。

  • Go: API、CLI、インフラツール
  • Rust: CLI、WASM、低レイヤー、高性能処理
観点gorust
得意領域API、CLI、運用安全性、性能、低レイヤー
学習初期は入りやすいが実務では設計力が必要周辺知識も含めて学ぶ
判断目的に合えば最初の候補チームや実行環境で選ぶ

追加調査で押さえる実務ポイント

GoとRustはどちらもnative executableを作れる現代的な言語ですが、最適化する対象が異なります。Goはgarbage collection、goroutine、channel、標準library、単純なbuildでnetwork serviceとteam開発を速くします。Rustはownershipとborrow checkerによりGCなしでmemory safetyを強制し、低レイヤー制御と予測可能なresource管理を重視します。性能の一般論ではなく、latency、memory、開発速度、unsafe境界で選びます。

先に結論

条件選びやすい言語
Web API・network serviceGo
CLIを短期間で開発Go
system componentRust
memory safety + no GCRust
学習・採用速度Go
embedded・WasmRust
大量goroutineGo
C/C++置換Rust

実行モデル

Go

sourceをnative binaryへcompileし、runtimeがGC、goroutine scheduler、stack growth等を管理します。

Rust

sourceをnative binaryへcompileし、ownership・lifetimeをcompile時に検査します。通常runtime GCはありません。

最小例

Go

package main

import "fmt"

func main() {
    fmt.Println("hello")
}

Rust

fn main() {
    println!("hello");
}

Memory管理

観点GoRust
基本GCownership
allocationescape analysis等explicit type/ownership
pauseGC影響ありGCなし
use-after-freeruntime管理compile-time防止
学習比較的容易ownership習得必要

Concurrency

Goはgoroutineとchannelを言語・runtimeへ統合します。RustはOS thread、async runtime、channel、mutexをtype systemのSendSyncと組み合わせます。

Goのgoroutineは軽量ですが、raceが自動的に消えるわけではありません。go test -race等を使います。

Rustはdata raceを多くの場面でcompile時に防ぎますが、deadlockやlogic raceは残ります。

Error handling

Goは複数戻り値のerrorを明示的に扱います。RustはResult<T, E>?で伝播します。

Buildと配布

Goは単一binaryを作りやすく、cross compileも比較的単純です。RustはCargo ecosystemが強力ですが、native dependencyとtarget toolchainを確認します。

性能比較の注意

benchmarkはworkload、allocator、GC setting、I/O、compiler version、libraryで変わります。自社taskでp50/p95 latency、memory、CPU、binary size、build timeを測ります。

Team判断

Goが向く

  • 多人数backend
  • microservice
  • infrastructure tool
  • simple deployment
  • standardized style
  • fast onboarding

Rustが向く

  • parser
  • database engine
  • network proxy
  • embedded
  • browser component
  • security-sensitive native code

両方使う

control planeをGo、performance-critical data planeをRustにする構成もあります。FFIよりprocess/API境界の方が保守しやすい場合があります。

次に読む

参考リンク