Skip to content
Skills Library
Programming · Conversion & formats

port-from-cpp

/port-from-cpp

Port C++ into Rust — RAII and smart pointers onto ownership, templates onto generics and traits, exceptions onto Result, the STL onto Rust collections, and the traps (move semantics, implicit conversions, undefined behaviour, iterator invalidation). Use when porting, rewriting, or migrating C++, a C++ library, or a C++ application into Rust, when replacing a C++ module with Rust behind the existing build, or when the user asks how a C++ construct such as unique_ptr, shared_ptr, a template, or an STL container translates to Rust.

Repository
skills ↗
Licence
BSD-3-Clause
Last repository change
16.08.2026

Assessment and security

95/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content30 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
4
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 16.08.2026, without references to retired models or APIs.

Licence

BSD-3-Clause, otwarta.

What it does and when to use it

Port C++ into Rust — RAII and smart pointers onto ownership, templates onto generics and traits, exceptions onto Result, the STL onto Rust collections, and the traps (move semantics, implicit conversions, undefined behaviour, iterator invalidation). Use when porting, rewriting, or migrating C++, a C++ library, or a C++ application into Rust, when replacing a C++ module with Rust behind the existing build, or when the user asks how a C++ construct such as unique_ptr, shared_ptr, a template, or an STL container translates to Rust.

Original author description

Port C++ into Rust — RAII and smart pointers onto ownership, templates onto generics and traits, exceptions onto Result, the STL onto Rust collections, and the traps (move semantics, implicit conversions, undefined behaviour, iterator invalidation). Use when porting, rewriting, or migrating C++, a C++ library, or a C++ application into Rust, when replacing a C++ module with Rust behind the existing build, or when the user asks how a C++ construct such as unique_ptr, shared_ptr, a template, or an STL container translates to Rust.

How it works

  1. A construct mapping and a boundary, nothing else
  2. The end state, and the C++ default
  3. The traps that break parity silently
  4. Mapping, in one line
  5. RAII maps, and this is the good news
  6. Templates are generics until they are not
  7. The test suite is the corpus, and the sanitizers are evidence
  8. Verification
Original SKILL.md headings · 97 lines, 963 words
SKILL.md preview
---
name: port-from-cpp
description: Port C++ into Rust — RAII and smart pointers onto ownership, templates onto generics and traits, exceptions onto Result, the STL onto Rust collections, and the traps (move semantics, implicit conversions, undefined behaviour, iterator invalidation). Use when porting, rewriting, or migrating C++, a C++ library, or a C++ application into Rust, when replacing a C++ module with Rust behind the existing build, or when the user asks how a C++ construct such as unique_ptr, shared_ptr, a template, or an STL container translates to Rust.
---

# Port from C++

## A construct mapping and a boundary, nothing else

This skill maps C++ to Rust and owns the C++ boundary. The parity
contract, the five phases, and the anti-patterns are `/port-to-rust`,
which runs first and stays running through the port. C is a separate
skill — the mechanics overlap, the idioms do not — so a C codebase
with a `.cpp` extension is still `/port-from-c` territory in everything
but the build.

## The end state, and the C++ default

`/port-to-rust` names three end states. In C++ terms: A, a standalone
Rust binary or library replacing the C++ entirely; B, a Rust core with
a permanent C++-callable surface, real when the deliverable is a
library other C++ code links; and C, a bridge that carries the
migration module by module and is deleted at cut-over. **C is the
default for C++**, more than for any other language in this set: a
large C++ codebase cannot be replaced in one step, and the seam — a
Rust `staticlib` linked into the existing build — lets leaf modules
move one at a time. The bridge is disposable scaffolding that must
still be *sound*, and that tension is what this skill manages; the
mechanics are in `BOUNDARY.md`.

## The traps that break parity silently

The body carries these five because each one produces a port that
passes a naive test suite and is wrong; the full table is `MAPPING.md`.

| Trap | What actually differs |
|---|---|
| Undefined behaviour was doing something | Signed overflow, strict aliasing violations, reading uninitialized memory, and out-of-bounds access are undefined in C++ and defined-or-rejected in Rust. Any behaviour the source got from UB is behaviour the port cannot reproduce and must not try to — it is found, recorded, and decided, and it is one of the few legitimate reasons for a port to differ from its source |
| Move semantics are not the same move | A C++ move leaves a valid, unspecified object behind and runs a destructor on it; a Rust move is a bitwise transfer with no hook and no leftover object. Code that reads a moved-from C++ object is doing something the port cannot express, and self-referential types that relied on move constructors need a different design entirely |
| Implicit conversions | C++ converts between numeric types, applies integer promotion, and calls converting constructors without a cast. Rust requires every conversion to be written, so a port surfaces conversions the source never made visible — and each one is a place a value could have been truncated silently |
| Iterator and reference invalidation | A `push_back` may reallocate and invalidate every outstanding iterator and reference; C++ tolerates the pattern until it does not. Rust rejects the pattern at compile time, so the port must restructure — usually with indices — and the restructuring is where the design gets clearer, not where it gets worse |
| Exceptions cross layers invisibly | An exception propagates through frames that never mention it, including destructors that run on the way out. Mapping to `Result` makes every propagation point visible and surfaces error paths the source never named — `/rust-errors`. Note the `noexcept` boundary: a C++ function that promises not to throw becomes an infallible signature, and one that did throw becomes `Result` |

## Mapping, in one line

`MAPPING.md` holds the full table; a row is a starting point, not a
rewrite rule. Where the source behaviour was undefined there is no
row — there is a contract decision.

## RAII maps, and this is the good news

Destructors map to `Drop`, `unique_ptr` to `Box`, `shared_ptr` to
`Arc` (or `Rc` where single-threaded), `weak_ptr` to `Weak`,
references to `&`/`&mut`, and `const` to immutability by default.
Reach for `Arc`/`Rc` only where the C++ genuinely shared ownership,
not everywhere a `shared_ptr` appeared — `shared_ptr` is often used
where a `unique_ptr` or a plain reference would have done, and
copying that habit into Rust produces the reference-counted soup
`/ownership-not-clone` exists to prevent. `Drop` cannot fail, so a
destructor that could throw becomes an explicit `close()`/`finish()`
returning `Result`.

## Templates are generics until they are not

Simple templates map to generics with trait bounds; SFINAE and
concepts map to bounds; CRTP usually maps to a plain trait; template
specialization maps to distinct trait impls. Where a template was doing
compile-time computation or reflection, the honest answer is often a
macro or a build script — a template metaprogram is usually
reimplemented rather than translated. Multiple inheritance and virtual
bases have no mapping and become composition.

## The test suite is the corpus, and the sanitizers are evidence

A GoogleTest or Catch2 suite is characterization input: the cases are
inputs, the assertions are the recorded behaviour. C++ ports have one
advantage no other language in this set does: running the source
under ASan, UBSan, and TSan before porting tells you which behaviours
are UB and therefore not part of the contract. Run it before any Rust
exists — the characterization pass of `/port-to-rust`; harness shapes
are `/rust-testing`.

## Verification

Both sides:

```bash
cargo test --all-features
cargo clippy --all-targets --all-features   # add -- -D warnings only if the target repo has no lint config
cargo miri test                              # any module with unsafe, including every FFI shim
ctest            # or the existing C++ test command — the source still passes while both run
```

Plus the source-side sanitizer run (`-fsanitize=address,undefined`),
whose findings belong in the contract as behaviour that is explicitly
not reproduced. Then the differential run over the recorded corpus,
reported with its denominator, plus the corpus requirement: at least
one case per trap — an input at a signed-overflow boundary, a
container operation that would have invalidated an iterator, a
narrowing conversion, and an input that threw.
Files in this package (4)
  • BOUNDARY.md 3 KB
  • MAPPING.md 5 KB
  • SKILL.md 6 KB
  • agents/openai.yaml 1 KB

Versions

The database has no other skills named port-from-cpp.

Database snapshot: 13.11.2026 · package version f36e1bcd43df (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 95cpp-smart-pointers
    Use when C++ smart pointers including unique_ptr, shared_ptr, and weak_ptr for automatic memory management following RAII principles.
  • 95port-from-java
    Port Java into Rust — class hierarchies onto enums and composition, exceptions onto Result, collections and streams onto Rust equivalents, and the traps (UTF-16 strings, silent integer wraparound, null, equals/hashCode contracts). Use when porting, rewriting, or migrating Java,…
  • 95port-from-c
    Port C into Rust — recovering the ownership, lifetime, nullability, and length that C never recorded, mapping pointers to references and slices, tag-plus-union to enums, return codes and errno to Result, and linking Rust into the existing build with bindgen and cbindgen. Use…
  • 90cpp-pro
    Write idiomatic C++ code with modern features, RAII, smart pointers, and STL algorithms. Handles templates, move semantics, and performance optimization. Use for C++ refactoring, memory safety, or complex C++ patterns.
  • 91cpp-smart-pointers
    Use when managing memory safely in C++ with smart pointers including unique_ptr, shared_ptr, weak_ptr, and RAII patterns.
  • 95lang-cpp
    Use this skill when the user says 'C++', 'cpp', 'modern C++', 'C++20', 'C++23', 'RAII', 'smart pointers', 'move semantics', 'templates', 'concepts', 'CMake', 'undefined behavior C++', 'rule of five', 'std::span', 'coroutines'. Writes and reviews modern C++ (C++17/20/23):…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.