본문으로 건너뛰기
토파즈문서
6.05.21

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    다운로드 →

    라이브러리 · 컬렉션

    Array.get for T

    Return the element at an index, or None() for a negative index or one at or past the length.

    get(i: int) -> Option<T>
    수신자
    Array<T>
    최소 인자 수
    1
    최대 인자 수
    1

    Declaration and parameters

    사용 조건

    기본 포함 · 예

    These types are provided by the language. No module import is required.

    The declarations below describe the standard operations on built-in values.

    수신자

    Array<T>

    매개변수 제약

    Array<T>.get(i: int) returns Option<T>. An index from zero to one less than the length returns Some with that element. A negative index or an index at or past the length returns None().

    get never faults for its index. Use values[i] when an index outside the array is a program error. That expression faults under the access rule.

    The length property returns the number of elements as int. An empty array has length 0.

    Array<T>.push(x: T) returns unit. It appends x after the last element and increases the length by one. Earlier elements keep their positions.

    Push writes to the array value. The receiver must be rooted in a let mut binding. Other array values are unchanged under collection value semantics.

    결과

    The declared result type is Option<T>.

    효과

    실패

    Result and effects

    The lookup never faults for its index. Index with values[i] when an index outside the array is a program error.

    Related entries

    Collections →

    Read, count and append array elements

    Read, count and append array elements

    Array<T>.get(i: int) returns Option<T>. An index from zero to one less than the length returns Some with that element. A negative index or an index at or past the length returns None().

    get never faults for its index. Use values[i] when an index outside the array is a program error. That expression faults under the access rule.

    The length property returns the number of elements as int. An empty array has length 0.

    Array<T>.push(x: T) returns unit. It appends x after the last element and increases the length by one. Earlier elements keep their positions.

    Push writes to the array value. The receiver must be rooted in a let mut binding. Other array values are unchanged under collection value semantics.

    이유

    A lookup that may miss returns its absence as a value, so reads of neighbors at the edges of a sequence need no separate bounds test. Indexing keeps its fault for indexes that a correct program never produces. The two intents keep two spellings. push builds a sequence in order without rebuilding it.

    Read elements without faulting at the edges

    main.tpzRead elements without faulting at the edges플레이그라운드 열기 →
    1. import std.io
    2. let names = ["a", "b", "c"]
    3. for index in ([-1, 0, 2, 3]) {
    4. match (names.get(index)) {
    5. case Some(name) => io.writeLine("{index}: {name}")
    6. case None() => io.writeLine("{index}: none")
    7. }
    8. }
    9. let empty: Array<string> = []
    10. io.writeLine("{empty.length}: {empty.get(0) ?? "none"}")
    기대 출력종료 코드 0
    -1: none
    0: a
    2: c
    3: none
    0: none
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. -1: none
    2. 0: a
    3. 2: c
    4. 3: none
    5. 0: none

    Append elements to a mutable array

    main.tpzAppend elements to a mutable array플레이그라운드 열기 →
    1. import std.io
    2. let mut names: Array<string> = []
    3. io.writeLine("{names.length}")
    4. names.push("a")
    5. names.push("b")
    6. io.writeLine("{names.length}: {names.join(",")}")
    기대 출력종료 코드 0
    0
    2: a,b
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 0
    2. 2: a,b