Starknet 백서

Starknet

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 73쪽 중 6쪽

// after the iterator is finished, so is peek() assert_eq!(iter.peek(), Option::None); assert_eq!(iter.next(), Option::None); ``` ### take Creates an iterator that yields the first `n` elements, or fewer if the underlying iterator ends sooner. `take(n)` yields elements until `n` elements are yielded or the end of the iterator is reached (whichever happens first). The returned iterator is a prefix of length `n` if the original iterator contains at least `n` elements, otherwise it contains all of the (fewer than `n`) elements of the original iterator. #### Signature ```rust theme={null} fn take(self: T, n: u32) -> Take ``` #### Examples Basic usage: ```rust theme={null} let mut iter = array![1, 2, 3].into_iter().take(2); assert_eq!(iter.next(), Some(1)); assert_eq!(iter.next(), Some(2)); assert_eq!(iter.next(), None); ``` If less than `n` elements are available, `take` will limit itself to the size of the underlying iterator: ```rust theme={null} let mut iter = array![1, 2].into_iter().take(5); assert_eq!(iter.next(), Some(1)); assert_eq!(iter.next(), Some(2)); assert_eq!(iter.next(), None); ``` ### sum Sums the elements of an iterator. Takes each element, adds them together, and returns the result. An empty iterator returns the zero value of the type. `sum()` can be used to sum any type implementing \[`Sum`]\[`core::iter::Sum`], including \[`Option`]\[`Option::sum`] and \[`Result`]\[`Result::sum`]. #### Panics When calling `sum()` and a primitive integer type is being returned, this method will panic if the computation overflows. #### Examples ```rust theme={null} let mut iter = array![1, 2, 3].into_iter(); let sum: usize = iter.sum(); assert_eq!(sum, 6); ``` #### Signature ```rust theme={null} fn sum, +Destruct, +Sum>(self: T) -> IteratorItem ``` ### product Iterates over the entire iterator, multiplying all the elements An empty iterator returns the one value of the type. #### Panics When calling `product()` and a primitive integer type is being returned, this method will panic if the computation overflows. #### Examples ```rust theme={null} fn factorial(n: u32) -> u32 { (1..=n).into_iter().product() } assert_eq!(factorial(0), 1); assert_eq!(factorial(1), 1); assert_eq!(factorial(5), 120); ``` #### Signature ```rust theme={null} fn product, +Destruct, +Product>(self: T) -> IteratorItem ``` ### chain Takes two iterators and creates a new iterator over both in sequence. `chain()` will return a new iterator which will first iterate over values from the first iterator and then over values from the second iterator. In other words, it links two iterators together, in a chain. 🔗 Arguments do not have to be of the same type as long as the underlying iterated over items are. #### Signature ```rust theme={null} fn chain, +TypeEqual, +Destruct, >( self: T, other: U, ) -> Chain ``` #### Examples Basic usage: ```rust theme={null} use core::ops::Range; let a: Array = array![7, 8, 9]; let b: Range = 0..5; let mut iter = a.into_iter().chain(b.into_iter()); assert_eq!(iter.next(), Option::Some(7)); assert_eq!(iter.next(), Option::Some(8)); assert_eq!(iter.next(), Option::Some(9)); assert_eq!(iter.next(), Option::Some(0)); assert_eq!(iter.next(), Option::Some(1)); assert_eq!(iter.next(), Option::Some(2)); assert_eq!(iter.next(), Option::Some(3)); assert_eq!(iter.next(), Option::Some(4)); assert_eq!(iter.next(), Option::None); ``` Since the argument to `chain()` uses [`IntoIterator`](./core-iter-traits-collect-IntoIterator), we can pass anything that can be converted into an [`Iterator`](./core-iter-traits-iterator-Iterator), not just an [`Iterator`](./core-iter-traits-iterator-Iterator) itself. For example, arrays implement [`IntoIterator`](./core-iter-traits-collect-IntoIterator), and so can be passed to `chain()` directly: ```rust theme={null} let a = array![1, 2, 3]; let b = array![4, 5, 6]; let mut iter = a.into_iter().chain(b); assert_eq!(iter.next(), Option::Some(1)); assert_eq!(iter.next(), Option::Some(2)); assert_eq!(iter.next(), Option::Some(3)); assert_eq!(iter.next(), Option::Some(4)); assert_eq!(iter.next(), Option::Some(5)); assert_eq!(iter.next(), Option::Some(6)); assert_eq!(iter.next(), Option::None); ``` ## Trait types ### Item The type of the elements being iterated over. #### Signature ```rust theme={null} type Item; ``` # core::keccak::cairo_keccak Source: https://docs.starknet.io/build/corelib/core-keccak-cairo_keccak Computes the Keccak-256 hash of a byte sequence with custom padding. This function allows hashing arbitrary byte sequences by providing the input as 64-bit words in little-endian format and a final partial word. ## Signature ```rust theme={null} pub fn cairo_keccak(ref input: Array, last_input_word: u64, last_input_num_bytes: u32) -> u256 ``` #### Arguments * `input` - Array of complete 64-bit words in little-endian format * `last_input_word` - Final partial word (if any) * `last_input_num_bytes` - Number of valid bytes in the final word (0-7) #### Returns The 32-byte Keccak-256 hash as a little-endian `u256` #### Panics Panics if `last_input_num_bytes` is greater than 7. ## Examples ```rust theme={null} use core::keccak::cairo_keccak; // Hash "Hello world!" by splitting into 64-bit words in little-endian let mut input = array![0x6f77206f6c6c6548]; // a full 8-byte word let hash = cairo_keccak(ref input, 0x21646c72, 4); // 4 bytes of the last word assert!(hash == 0xabea1f2503529a21734e2077c8b584d7bee3f45550c2d2f12a198ea908e1d0ec); ``` # core::keccak::compute_keccak_byte_array Source: https://docs.starknet.io/build/corelib/core-keccak-compute_keccak_byte_array Computes the Keccak-256 hash of a `ByteArray`. ## Signature ```rust theme={null} pub fn compute_keccak_byte_array(arr: ByteArray) -> u256 ``` #### Arguments * `arr` - The input bytes to hash #### Returns The 32-byte Keccak-256 hash as a little-endian `u256` ## Examples ```rust theme={null} use core::keccak::compute_keccak_byte_array; let text: ByteArray = "Hello world!"; let hash = compute_keccak_byte_array(@text); assert!(hash == 0xabea1f2503529a21734e2077c8b584d7bee3f45550c2d2f12a198ea908e1d0ec); ``` # core::keccak::keccak_u256s_be_inputs Source: https://docs.starknet.io/build/corelib/core-keccak-keccak_u256s_be_inputs Computes the Keccak-256 hash of multiple `u256` values in big-endian format. ## Signature ```rust theme={null} pub fn keccak_u256s_be_inputs(mut input: Span) -> u256 ``` #### Arguments * `input` - A span of big-endian `u256` values to be hashed #### Returns The 32-byte Keccak-256 hash as a little-endian `u256` ## Examples ```rust theme={null} use core::keccak::keccak_u256s_be_inputs; let input = array![0x1234_u256, 0x5678_u256].span(); let hash = assert!(keccak_u256s_be_inputs(input) == 0xfa31cb2326ed629f79d2da5beb78e2bd8ac7a1b8b86cae09eeb6a89a908b12a); ``` # core::keccak::keccak_u256s_le_inputs Source: https://docs.starknet.io/build/corelib/core-keccak-keccak_u256s_le_inputs Computes the Keccak-256 hash of multiple `u256` values in little-endian format. ## Signature ```rust theme={null} pub fn keccak_u256s_le_inputs(mut input: Span) -> u256 ``` #### Arguments * `input` - A span of little-endian `u256` values to be hashed #### Returns The 32-byte Keccak-256 hash as a little-endian `u256` ## Examples ```rust theme={null} use core::keccak::keccak_u256s_le_inputs; let input: Span = array![0, 1, 2].span(); assert!(keccak_u256s_le_inputs(input) == 0xf005473605efc7d8ff67d9f23fe2e4a4f23454c12b49b38822ed362e0a92a0a6); ``` # core::math::egcd Source: https://docs.starknet.io/build/corelib/core-math-egcd Computes the extended GCD and Bezout coefficients for two numbers. Uses the Extended Euclidean algorithm to find (g, s, t, sub\_direction) where `g = gcd(a, b)`. The relationship between inputs and outputs is: * If `sub_direction` is true: `g = s * a - t * b` * If `sub_direction` is false: `g = t * b - s * a` Returns a tuple (g, s, t, sub\_direction) where g is the GCD and `(s, -t)` or `(-s, t)` are the Bezout coefficients (according to `sub_direction`). ## Signature ```rust theme={null} pub fn egcd, +Drop, +Add, +Mul, +DivRem, +core::num::traits::Zero, +core::num::traits::One, +TryInto>, >( a: NonZero, b: NonZero, ) -> (T, T, T, bool) ``` ## Examples ```rust theme={null} use core::math::egcd; let (g, s, t, dir) = egcd::(12, 8); assert!(g == 4); ``` # core::math::inv_mod Source: https://docs.starknet.io/build/corelib/core-math-inv_mod Computes the modular multiplicative inverse of `a` modulo `n`. Returns `s` such that `a*s ≡ 1 (mod n)` where `s` is between `1` and `n-1` inclusive, or `None` if `gcd(a,n) > 1` (inverse doesn't exist). ## Signature ```rust theme={null} pub fn inv_mod, +Drop, +Add, +Sub, +Mul, +DivRem, +core::num::traits::Zero, +core::num::traits::One, +TryInto>, >( a: NonZero, n: NonZero, ) -> Option ``` ## Examples ```rust theme={null} use core::math::inv_mod; let inv = inv_mod::(3, 7); assert!(inv == Some(5)); ``` # core::math::u256_div_mod_n Source: https://docs.starknet.io/build/corelib/core-math-u256_div_mod_n Returns `a / b (mod n)`, or `None` if `b` is not invertible modulo `n`. ## Signature ```rust theme={null} pub fn u256_div_mod_n(a: u256, b: u256, n: NonZero) -> Option ``` ## Examples ```rust theme={null} use core::math::u256_inv_mod; let result = u256_div_mod_n(17, 7, 29); assert!(result == Some(19)); ``` # core::math::u256_inv_mod Source: https://docs.starknet.io/build/corelib/core-math-u256_inv_mod Returns the inverse of `a` modulo `n`, or `None` if `a` is not invertible modulo `n`. All `a`s will be considered not invertible for `n == 1`. ## Signature ```rust theme={null} pub fn u256_inv_mod(a: u256, n: NonZero) -> Option> ``` ## Examples ```rust theme={null} use core::math::u256_inv_mod; let inv = u256_inv_mod(3, 17); assert!(inv == Some(6)); ``` # core::math::u256_mul_mod_n Source: https://docs.starknet.io/build/corelib/core-math-u256_mul_mod_n Returns `a * b (mod n)`. ## Signature ```rust theme={null} pub fn u256_mul_mod_n(a: u256, b: u256, n: NonZero) -> u256 ``` ## Examples ```rust theme={null} use core::math::u256_mul_mod_n; let result = u256_mul_mod_n(17, 23, 29); assert!(result == 14); ``` # core::metaprogramming::TypeEqual Source: https://docs.starknet.io/build/corelib/core-metaprogramming-TypeEqual A trait that can be used to disable implementations based on the types of the generic args. Assumes that `TypeEqualImpl` is the only implementation of this trait. Primarily used for optimizations by enabling type-specific implementations. Since `TypeEqualImpl` is the only implementation, adding `-TypeEqual` as a trait bound ensures the implementation is only available when T and U are different types. ## Signature ```rust theme={null} pub trait TypeEqual ``` # core::never Source: https://docs.starknet.io/build/corelib/core-never ## Signature ```rust theme={null} pub enum never {} ``` # core::nullable::FromNullableResult Source: https://docs.starknet.io/build/corelib/core-nullable-FromNullableResult Represents the result of matching a `Nullable` value. Used to safely handle both null and non-null cases when using `match_nullable` on a `Nullable`. ## Signature ```rust theme={null} pub enum FromNullableResult { Null, NotNull: Box, } ``` ## Variants ### Null Represents a null value #### Signature ```rust theme={null} Null ``` ### NotNull The boxed value when not null #### Signature ```rust theme={null} NotNull: Box ``` # core::nullable::Nullable Source: https://docs.starknet.io/build/corelib/core-nullable-Nullable A type that can either be null or contain a boxed value. ## Signature ```rust theme={null} pub extern type Nullable; ``` # core::nullable::NullableTrait Source: https://docs.starknet.io/build/corelib/core-nullable-NullableTrait ## Signature ```rust theme={null} pub trait NullableTrait ``` ## Trait functions ### deref Wrapper for `Deref::deref`. Prefer using `Deref::deref` directly. This function exists for backwards compatibility. #### Signature ```rust theme={null} fn deref(nullable: Nullable) -> T ``` #### Examples Preferred way: ```rust theme={null} let value: Nullable = NullableTrait::new(42); let unwrapped = value.deref(); ``` This function method does the same thing: ```rust theme={null} use core::nullable::NullableTrait; let also_unwrapped = NullableTrait::deref(value); ``` ### deref\_or Returns the contained value if not null, or returns the provided default value. #### Signature ```rust theme={null} fn deref_or>(self: Nullable, default: T) -> T ``` #### Examples ```rust theme={null} let value: Nullable = NullableTrait::new(42); assert!(value.deref_or(0) == 42); let null_value: Nullable = Default::default(); assert!(null_value.deref_or(0) == 0); ``` ### deref\_or\_else Returns the contained value if not null, or computes it from a closure. #### Signature ```rust theme={null} fn deref_or_else, impl func: FnOnce, +Drop>( self: Nullable, f: F, ) -> T ``` #### Examples ```rust theme={null} let value: Nullable = NullableTrait::new(42); assert!(value.deref_or_else(|| 0) == 42); let null_value: Nullable = Default::default(); assert!(null_value.deref_or_else(|| 0) == 0); ``` ### new Creates a new non-null `Nullable` with the given value. #### Signature ```rust theme={null} fn new(value: T) -> Nullable ``` #### Examples ```rust theme={null} let value: Nullable = NullableTrait::new(42); assert!(!value.is_null()); ``` ### is\_null Returns `true` if the value is null. #### Signature ```rust theme={null} fn is_null(self: @Nullable) -> bool ``` #### Examples ```rust theme={null} let value: Nullable = NullableTrait::new(42); assert!(!value.is_null()); let null_value: Nullable = Default::default(); assert!(null_value.is_null()); ``` ### as\_snapshot Creates a new `Nullable` containing a snapshot of the value. This is useful when working with non-copyable types inside a `Nullable`. This allows you to keep using the original value while also having access to a snapshot of it, preventing the original value from being moved. #### Signature ```rust theme={null} fn as_snapshot(self: @Nullable) -> Nullable ``` #### Examples ```rust theme={null} let value: Nullable> = NullableTrait::new(array![1, 2, 3]); let res = (@value).as_snapshot(); assert!(res.deref() == @array![1, 2, 3]); assert!(value.deref() == array![1, 2, 3]); ``` # core::nullable::match_nullable Source: https://docs.starknet.io/build/corelib/core-nullable-match_nullable ## Signature ```rust theme={null} pub extern fn match_nullable(value: Nullable) -> FromNullableResult nopanic; ``` # core::nullable::null Source: https://docs.starknet.io/build/corelib/core-nullable-null ## Signature ```rust theme={null} pub extern fn null() -> Nullable nopanic; ``` # core::num::traits::bit_size::BitSize Source: https://docs.starknet.io/build/corelib/core-num-traits-bit_size-BitSize A trait used to retrieve the size of a type in bits. ## Signature ```rust theme={null} pub trait BitSize ``` ## Trait functions ### bits Returns the bit size of `T` as a `usize`. #### Signature ```rust theme={null} fn bits() -> u32 ``` #### Examples ```rust theme={null} use core::num::traits::BitSize; let bits = BitSize::::bits(); assert!(bits == 8); ``` # core::num::traits::bounded::Bounded Source: https://docs.starknet.io/build/corelib/core-num-traits-bounded-Bounded A trait defining minimum and maximum bounds for numeric types. This trait only supports types that can have constant values. ## Signature ```rust theme={null} pub trait Bounded ``` ## Trait constants ### MIN Returns the minimum value for type `T`. #### Signature ```rust theme={null} const MIN: T; ``` #### Examples ```rust theme={null} use core::num::traits::Bounded; let min = Bounded::::MIN; assert!(min == 0); ``` ### MAX Returns the maximum value for type `T`. #### Signature ```rust theme={null} const MAX: T; ``` #### Examples ```rust theme={null} use core::num::traits::Bounded; let max = Bounded::::MAX; assert!(max == 255); ``` # core::num::traits::one::One Source: https://docs.starknet.io/build/corelib/core-num-traits-one-One Defines a multiplicative identity element for `T`. ## Signature ```rust theme={null} pub trait One ``` # Laws ```text theme={null} a * 1 = a ∀ a ∈ T 1 * a = a ∀ a ∈ T ``` ## Trait functions ### one Returns the multiplicative identity element of `T`, `1`. #### Signature ```rust theme={null} fn one() -> T ``` #### Examples ```rust theme={null} use core::num::traits::One; assert!(One::::one() == 1); ``` ### is\_one Returns true if `self` is equal to the multiplicative identity. #### Signature ```rust theme={null} fn is_one(self: @T) -> bool ``` #### Examples ```rust theme={null} use core::num::traits::One; assert!(1.is_one()); assert!(!0.is_one()); ``` ### is\_non\_one Returns false if `self` is equal to the multiplicative identity. #### Signature ```rust theme={null} fn is_non_one(self: @T) -> bool ``` #### Examples ```rust theme={null} use core::num::traits::One; assert!(0.is_non_one()); assert!(!1.is_non_one()); ``` # core::num::traits::ops::checked::CheckedAdd Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-checked-CheckedAdd Performs addition that returns `None` instead of wrapping around on overflow. ## Signature ```rust theme={null} pub trait CheckedAdd ``` ## Examples ```rust theme={null} use core::num::traits::CheckedAdd; let result = 1_u8.checked_add(2); assert!(result == Some(3)); let result = 255_u8.checked_add(1); assert!(result == None); // Overflow ``` ## Trait functions ### checked\_add Adds two numbers, checking for overflow. If overflow happens, `None` is returned. #### Signature ```rust theme={null} fn checked_add(self: T, v: T) -> Option ``` # core::num::traits::ops::divrem::DivRem Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-divrem-DivRem Performs truncated division and remainder. `T` – dividend type (left-hand operand) `U` – divisor type (right-hand operand, must be wrapped in `NonZero` at call-site) The division truncates toward zero, like Cairo’s `/` and `%`. ## Signature ```rust theme={null} pub trait DivRem ``` # Associated types * `Quotient` – the type produced by the division * `Remainder` – the type produced by the modulo ## Examples Identical operand types: ```rust theme={null} use core::traits::{DivRem, NonZero}; let lhs: u32 = 7; let rhs: NonZero = 3.try_into().unwrap(); assert!(DivRem::::div_rem(lhs, rhs) == (2, 1)); ``` Heterogeneous division (`u256` by `u128`): ```rust theme={null} use core::traits::DivRem; use integer::u256_as_non_zero; let big: u256 = 1_000_000; // dividend let nz10: NonZero = u256_as_non_zero(10_u128.into()); // divisor let (q, r) = DivRem::::div_rem(big, nz10); // q : u256, r : u128 ``` ## Trait functions ### div\_rem Computes both `/` and `%` in a single pass. #### Signature ```rust theme={null} fn div_rem(self: T, other: NonZero) -> (DivRemQuotient, DivRemRemainder) ``` ## Trait types ### Quotient Quotient returned by the division. #### Signature ```rust theme={null} type Quotient; ``` ### Remainder Remainder returned by the modulo operation. #### Signature ```rust theme={null} type Remainder; ``` # core::num::traits::ops::overflowing::OverflowingAdd Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-overflowing-OverflowingAdd Performs addition with a flag for overflow. ## Signature ```rust theme={null} pub trait OverflowingAdd ``` ## Examples ```rust theme={null} use core::num::traits::OverflowingAdd; let (result, is_overflow) = 1_u8.overflowing_add(255_u8); assert!(result == 0); assert!(is_overflow); ``` ## Trait functions ### overflowing\_add Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned. #### Signature ```rust theme={null} fn
프로젝트로 돌아가기