From 9f89174ec62db63663eb9ab9f5cb2c22a6366967 Mon Sep 17 00:00:00 2001 From: Conor Bronsdon <120674402+conorbronsdon@users.noreply.github.com> Date: Thu, 26 Mar 2026 23:05:53 -0700 Subject: [PATCH] fix(mojo-syntax): add string indexing and universal std. prefix to removed-syntax table String indexing with byte= keyword and stdlib import prefix are the most common compilation failures for Python developers new to Mojo. This adds them prominently to the removed-syntax table and Strings section. Findings from a structured skills evaluation building a real CLI project. --- mojo-syntax/SKILL.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mojo-syntax/SKILL.md b/mojo-syntax/SKILL.md index 90abb01..24a7db8 100644 --- a/mojo-syntax/SKILL.md +++ b/mojo-syntax/SKILL.md @@ -41,6 +41,11 @@ This skill specifically works on the latest Mojo, and stable versions may differ | `@register_passable("trivial")` | `TrivialRegisterPassable` trait | | `@register_passable` | `RegisterPassable` trait | | `Stringable` / `__str__` | `Writable` / `write_to` | +| `s[i]` | `s[byte=i]` — returns `StringSlice`; wrap in `String()` if needed | +| `s[0:10]`, `s[:5]` | No slice syntax on String — use `s.codepoint_slices()` or Python FFI | +| `from sys import ...` | `from std.sys import ...` | +| `from os import ...` | `from std.os import ...` | +| `from pathlib import ...` | `from std.pathlib import ...` | | `from collections import ...` | `from std.collections import ...` | | `from memory import ...` | `from std.memory import ...` | | `constrained(cond, msg)` | `comptime assert cond, msg` | @@ -184,6 +189,8 @@ var result = MyStruct(headers=d.copy()) # or: headers=d^ ## Imports use `std.` prefix +**All stdlib imports require the `std.` prefix.** The removed-syntax table shows the most common corrections, but the rule is universal. + ```mojo from std.testing import assert_equal, TestSuite from std.algorithm import vectorize @@ -365,6 +372,27 @@ v.reduce_min() # horizontal min → Scalar `len(s)` returns **byte length**, not codepoint count. Mojo strings are UTF-8. Byte indexing requires keyword syntax: `s[byte=idx]` (not `s[idx]`). +### String indexing (common error) + +```mojo +# WRONG — compile error +var ch = s[0] +var sub = s[0:10] + +# CORRECT — byte-level access +var ch = s[byte=0] # returns StringSlice +var ch_str = String(s[byte=0]) # if you need a String + +# CORRECT — iterate codepoints for truncation +var result = String("") +var count = 0 +for cp in s.codepoint_slices(): + if count >= 10: + break + result += String(cp) + count += 1 +``` + ```mojo var s = "Hello" len(s) # 5 (bytes)