Movatterモバイル変換


[0]ホーム

URL:


Keyboard shortcuts

Press or to navigate between chapters

PressS or/ to search in the book

Press? to show this help

PressEsc to hide this help

The Rust Reference

    Array and array index expressions

    Array expressions

    Syntax
    ArrayExpression[ArrayElements?]

    ArrayElements
          Expression (,Expression )*,?
        |Expression;Expression

    Array expressions constructarrays.Array expressions come in two forms.

    The first form lists out every value in the array.

    The syntax for this form is a comma-separated list of expressions of uniform type enclosed in square brackets.

    This produces an array containing each of these values in the order they are written.

    The syntax for the second form is two expressions separated by a semicolon (;) enclosed in square brackets.

    The expression before the; is called therepeat operand.

    The expression after the; is called thelength operand.

    The length operand must either be aninferred const or be aconstant expression of typeusize (e.g. aliteral or aconstant item).

    #![allow(unused)]fn main() {const C: usize = 1;let _: [u8; C] = [0; 1]; // Literal.let _: [u8; C] = [0; C]; // Constant item.let _: [u8; C] = [0; _]; // Inferred const.let _: [u8; C] = [0; (((_)))]; // Inferred const.}

    Note

    In an array expression, aninferred const is parsed as anexpression but then semantically treated as a separate kind ofconst generic argument.

    An array expression of this form creates an array with the length of the value of the length operand with each element being a copy of the repeat operand.That is,[a; b] creates an array containingb copies of the value ofa.

    If the length operand has a value greater than 1 then this requires the repeat operand to have a type that implementsCopy, to be aconst block expression, or to be apath to a constant item.

    When the repeat operand is a const block or a path to a constant item, it is evaluated the number of times specified in the length operand.

    If that value is0, then the const block or constant item is not evaluated at all.

    For expressions that are neither a const block nor a path to a constant item, it is evaluated exactly once, and then the result is copied the length operand’s value times.

    #![allow(unused)]fn main() {[1, 2, 3, 4];["a", "b", "c", "d"];[0; 128];              // array with 128 zeros[0u8, 0u8, 0u8, 0u8,];[[1, 0, 0], [0, 1, 0], [0, 0, 1]]; // 2D arrayconst EMPTY: Vec<i32> = Vec::new();[EMPTY; 2];}

    Array and slice indexing expressions

    Syntax
    IndexExpressionExpression[Expression]

    Array andslice-typed values can be indexed by writing a square-bracket-enclosed expression of typeusize (the index) after them.When the array is mutable, the resultingmemory location can be assigned to.

    For other types an index expressiona[b] is equivalent to*std::ops::Index::index(&a, b), or*std::ops::IndexMut::index_mut(&mut a, b) in a mutable place expression context.Just as with methods, Rust will also insert dereference operations ona repeatedly to find an implementation.

    Indices are zero-based for arrays and slices.

    Array access is aconstant expression, so bounds can be checked at compile-time with a constant index value.Otherwise a check will be performed at run-time that will put the thread in apanicked state if it fails.

    #![allow(unused)]fn main() {// lint is deny by default.#![warn(unconditional_panic)]([1, 2, 3, 4])[2];        // Evaluates to 3let b = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];b[1][2];                  // multidimensional array indexinglet x = (["a", "b"])[10]; // warning: index out of boundslet n = 10;let y = (["a", "b"])[n];  // panicslet arr = ["a", "b"];arr[10];                  // warning: index out of bounds}

    The array index expression can be implemented for types other than arrays and slices by implementing theIndex andIndexMut traits.


    [8]ページ先頭

    ©2009-2025 Movatter.jp