渲染zh-Hans

借自

Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans pack; supporting prose may still be English.

Introduces borrowed iteration or borrowed parameters.

Syntax: 借自 <expression>

Category#

iteration

Examples#

radix/corpus/de/de.fab (canonical · keyword)#

Introduces borrowed iteration or borrowed parameters.

# =============================================================================
# 借自 — Introduces borrowed iteration or borrowed parameters.
# =============================================================================
#
# What this teaches:
#   • Borrowed iteration — `遍历 借自 <expr> 常量 <ident> { ... }` iterates over a collection without taking ownership.
#   • Tabula key iteration — iterating over 映射 keys via borrowed iteration.
#   • Index iteration — iterating over 列表 indices via `借自`.
#
# Common mistakes:
#   • Passing a `借自` parameter to an `传入` or `取自` position — mode mismatch; cannot pass a read-only reference where mutation or consumption is expected (SEM057).
#
# See also: 取自, in
# =============================================================================

# 借自 — borrowed iteration / parameters
#
# GRAMMAR:
#   forDeStmt :← '遍历' '借自' expr '常量' ident block
#
# EXPECTED OUTPUT:
#   Scalar stdout smoke (see body).
#
# BACKEND:
#   Cross-ref: 遍历/de.fab.
#

main {
    const map<string, int> persona ← { "nomen": 1, "aetas": 30 }
    for ref persona const clavis {
        print clavis
    }
    const list<int, 3> numeri ← [10, 20, 30]
    for ref numeri const index {
        print index
    }
}

radix/corpus/itera/de.fab (canonical · keyword)#

Introduces borrowed iteration or borrowed parameters.

# =============================================================================
# 借自 — Introduces borrowed iteration or borrowed parameters.
# =============================================================================
#
# What this teaches:
#   • borrowed iteration (`遍历 借自`) over 映射 keys and 列表 indices — iterates without consuming the collection
#   • accessing values by key or index within a borrowed loop
#
# Common mistakes:
#   • Using 借自 (key/index iteration) when 取自 (value iteration) is needed, or vice versa — 借自 iterates keys/indices, 取自 iterates values.
#
# See also: 取自, in
# =============================================================================

# 遍历 借自 — for-in key and index iteration
#
# 遍历 借自 <对象> 常量 <key> { <body> }
# 遍历 借自 <列表> 常量 <index> { <body> }
#
# GRAMMAR:
#   forDeStmt :← '遍历' '借自' expr '常量' ident block
#
# EXPECTED OUTPUT:
#   none — stdout not pinned for this exemplum.

main {
    # Iterate over 映射 keys
    const map<string, string> persona ← { "nomen": "Marcus", "urbs": "Roma" }
    for ref persona const clavis {
        print clavis
    }

    # Access values using the key
    for ref persona const clavis {
        print clavis, persona[clavis]
    }

    # Iterate over 列表 indices
    const list<int, 3> numeri ← [10, 20, 30]
    for ref numeri const index {
        print "index: § valor: §"(index, numeri[index])
    }

    # With numerus-valued 映射
    const map<string, int> forma ← { "alpha": 1, "beta": 2 }
    for ref forma const k {
        print k
    }
}