Starknet 백서

Starknet

원문

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

overflowing_add(self: T, v: T) -> (T, bool) ``` # core::num::traits::ops::overflowing::OverflowingMul Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-overflowing-OverflowingMul Performs multiplication with a flag for overflow. ## Signature ```rust theme={null} pub trait OverflowingMul ``` ## Examples ```rust theme={null} use core::num::traits::OverflowingMul; let (result, is_overflow) = 1_u8.overflowing_mul(2_u8); assert!(result == 2); assert!(!is_overflow); ``` ## Trait functions ### overflowing\_mul Returns a tuple of the product 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 overflowing_mul(self: T, v: T) -> (T, bool) ``` # core::num::traits::ops::pow::Pow Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-pow-Pow Raises a value to the power of `exp`. Note that `0⁰` (`pow(0, 0)`) returns `1`. Mathematically this is undefined. ## Signature ```rust theme={null} pub trait Pow ``` #### Panics Panics if the result of the exponentiation operation overflows the output type. ## Examples ```rust theme={null} use core::num::traits::Pow; assert!(2_i8.pow(4_usize) == 16_i8); assert!(6_u8.pow(3_usize) == 216_u8); assert!(0_u8.pow(0_usize) == 1_u8); ``` ## Trait functions ### pow Returns `self` to the power `exp`. #### Signature ```rust theme={null} fn pow(self: Base, exp: Exp) -> PowOutput ``` ## Trait types ### Output The type of the result of the power calculation. #### Signature ```rust theme={null} type Output; ``` # core::num::traits::ops::saturating::SaturatingAdd Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-saturating-SaturatingAdd Performs addition that saturates at the numeric bounds instead of overflowing. ## Signature ```rust theme={null} pub trait SaturatingAdd ``` ## Examples ```rust theme={null} use core::num::traits::SaturatingAdd; assert!(255_u8.saturating_add(1_u8) == 255); ``` ## Trait functions ### saturating\_add Saturating addition. Computes `self + other`, saturating at the relevant high or low boundary of the type. #### Signature ```rust theme={null} fn saturating_add(self: T, other: T) -> T ``` # core::num::traits::ops::saturating::SaturatingMul Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-saturating-SaturatingMul Performs multiplication that saturates at the numeric bounds instead of overflowing. ## Signature ```rust theme={null} pub trait SaturatingMul ``` ## Examples ```rust theme={null} use core::num::traits::SaturatingMul; assert!(100_u8.saturating_mul(3_u8) == 255); ``` ## Trait functions ### saturating\_mul Saturating multiplication. Computes `self * other`, saturating at the relevant high or low boundary of the type. #### Signature ```rust theme={null} fn saturating_mul(self: T, other: T) -> T ``` # core::num::traits::ops::saturating::SaturatingSub Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-saturating-SaturatingSub Performs subtraction that saturates at the numeric bounds instead of overflowing. ## Signature ```rust theme={null} pub trait SaturatingSub ``` ## Examples ```rust theme={null} use core::num::traits::SaturatingSub; assert!(1_u8.saturating_sub(2_u8) == 0); ``` ## Trait functions ### saturating\_sub Saturating subtraction. Computes `self - other`, saturating at the relevant high or low boundary of the type. #### Signature ```rust theme={null} fn saturating_sub(self: T, other: T) -> T ``` # core::num::traits::ops::sqrt::Sqrt Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-sqrt-Sqrt A trait for computing the square root of a number. ## Signature ```rust theme={null} pub trait Sqrt ``` ## Examples ```rust theme={null} use core::num::traits::Sqrt; assert!(9_u8.sqrt() == 3); ``` ## Trait functions ### sqrt Computes the square root of a number. #### Signature ```rust theme={null} fn sqrt(self: T) -> SqrtTarget ``` ## Trait types ### Target The type of the result of the square root operation. #### Signature ```rust theme={null} type Target; ``` # core::num::traits::ops::widesquare::WideSquare Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-widesquare-WideSquare A trait for a type that can be squared to produce a wider type. This trait enables squaring operations where the result type has double the bit width of the input type, preventing overflow in cases where the result would exceed the input type's maximum value. ## Signature ```rust theme={null} pub trait WideSquare ``` ## Examples ```rust theme={null} use core::num::traits::WideSquare; let a: u8 = 16; let result: u16 = a.wide_square(); assert!(result == 256); ``` ## Trait functions ### wide\_square Calculates the square, producing a wider type. #### Signature ```rust theme={null} fn wide_square(self: T) -> WideSquareTarget ``` ## Trait types ### Target The type of the result of the square. #### Signature ```rust theme={null} type Target; ``` # core::num::traits::ops::wrapping::WrappingAdd Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-wrapping-WrappingAdd Performs addition that wraps around on overflow. ## Signature ```rust theme={null} pub trait WrappingAdd ``` ## Examples ```rust theme={null} use core::num::traits::WrappingAdd; let result = 255_u8.wrapping_add(1); assert!(result == 0); let result = 100_u8.wrapping_add(200); assert!(result == 44); // (100 + 200) % 256 = 44 ``` ## Trait functions ### wrapping\_add Wrapping (modular) addition. Computes `self + other`, wrapping around at the boundary of the type. #### Signature ```rust theme={null} fn wrapping_add(self: T, v: T) -> T ``` # core::num::traits::ops::wrapping::WrappingMul Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-wrapping-WrappingMul Performs multiplication that wraps around on overflow. ## Signature ```rust theme={null} pub trait WrappingMul ``` ## Examples ```rust theme={null} use core::num::traits::WrappingMul; let result = 10_u8.wrapping_mul(30); assert!(result == 44); // (10 * 30) % 256 = 44 let result = 200_u8.wrapping_mul(2); assert!(result == 144); // (200 * 2) % 256 = 144 ``` ## Trait functions ### wrapping\_mul Wrapping (modular) multiplication. Computes `self * other`, wrapping around at the boundary of the type. #### Signature ```rust theme={null} fn wrapping_mul(self: T, v: T) -> T ``` # core::num::traits::ops::wrapping::WrappingSub Source: https://docs.starknet.io/build/corelib/core-num-traits-ops-wrapping-WrappingSub Performs subtraction that wraps around on overflow. ## Signature ```rust theme={null} pub trait WrappingSub ``` ## Examples ```rust theme={null} use core::num::traits::WrappingSub; let result = 0_u8.wrapping_sub(1); assert!(result == 255); let result = 100_u8.wrapping_sub(150); assert!(result == 206); ``` ## Trait functions ### wrapping\_sub Wrapping (modular) subtraction. Computes `self - other`, wrapping around at the boundary of the type. #### Signature ```rust theme={null} fn wrapping_sub(self: T, v: T) -> T ``` # core::num::traits::zero::Zero Source: https://docs.starknet.io/build/corelib/core-num-traits-zero-Zero Defines an additive identity element for `T`. ## Signature ```rust theme={null} pub trait Zero ``` # Laws ```text theme={null} a + 0 = a ∀ a ∈ T 0 + a = a ∀ a ∈ T ``` ## Trait functions ### zero Returns the additive identity element of `T`, `0`. #### Signature ```rust theme={null} fn zero() -> T ``` #### Examples ```rust theme={null} use core::num::traits::Zero; assert!(Zero::::zero() == 0); ``` ### is\_zero Returns true if `self` is equal to the additive identity. #### Signature ```rust theme={null} fn is_zero(self: @T) -> bool ``` #### Examples ```rust theme={null} use core::num::traits::Zero; assert!(0.is_zero()); assert!(!5.is_zero()); ``` ### is\_non\_zero Returns false if `self` is equal to the additive identity. #### Signature ```rust theme={null} fn is_non_zero(self: @T) -> bool ``` #### Examples ```rust theme={null} use core::num::traits::Zero; assert!(5.is_non_zero()); assert!(!0.is_non_zero()); ``` # core::ops::arith::AddAssign Source: https://docs.starknet.io/build/corelib/core-ops-arith-AddAssign The addition assignment operator `+=`. ## Signature ```rust theme={null} pub trait AddAssign ``` ## Trait functions ### add\_assign Performs the `+=` operation. #### Signature ```rust theme={null} fn add_assign(ref self: Lhs, rhs: Rhs) ``` #### Examples ```rust theme={null} let mut x: u8 = 3; x += x; assert!(x == 6); ``` # core::ops::arith::DivAssign Source: https://docs.starknet.io/build/corelib/core-ops-arith-DivAssign The division assignment operator `/=`. ## Signature ```rust theme={null} pub trait DivAssign ``` ## Trait functions ### div\_assign Performs the `/=` operation. #### Signature ```rust theme={null} fn div_assign(ref self: Lhs, rhs: Rhs) ``` #### Examples ```rust theme={null} let mut x: u8 = 3; x /= x; assert!(x == 1); ``` # core::ops::arith::MulAssign Source: https://docs.starknet.io/build/corelib/core-ops-arith-MulAssign The multiplication assignment operator `*=`. ## Signature ```rust theme={null} pub trait MulAssign ``` ## Trait functions ### mul\_assign Performs the `*=` operation. #### Signature ```rust theme={null} fn mul_assign(ref self: Lhs, rhs: Rhs) ``` #### Examples ```rust theme={null} let mut x: u8 = 3; x *= x; assert!(x == 9); ``` # core::ops::arith::RemAssign Source: https://docs.starknet.io/build/corelib/core-ops-arith-RemAssign The remainder assignment operator `%=`. ## Signature ```rust theme={null} pub trait RemAssign ``` ## Trait functions ### rem\_assign Performs the `%=` operation. #### Signature ```rust theme={null} fn rem_assign(ref self: Lhs, rhs: Rhs) ``` #### Examples ```rust theme={null} let mut x: u8 = 3; x %= x; assert!(x == 0); ``` # core::ops::arith::SubAssign Source: https://docs.starknet.io/build/corelib/core-ops-arith-SubAssign The subtraction assignment operator `-=`. ## Signature ```rust theme={null} pub trait SubAssign ``` ## Trait functions ### sub\_assign Performs the `-=` operation. #### Signature ```rust theme={null} fn sub_assign(ref self: Lhs, rhs: Rhs) ``` #### Examples ```rust theme={null} let mut x: u8 = 3; x -= x; assert!(x == 0); ``` # core::ops::deref::Deref Source: https://docs.starknet.io/build/corelib/core-ops-deref-Deref A trait for dereferencing a value to provide transparent access to its contents. Implementing this trait allows a type to behave like its inner type, enabling direct access to the inner type's fields. Note: The `Deref` mechanism is limited and cannot be used to implicitly convert a type to its target type when passing arguments to functions. For example, if you have a function that takes an `Inner`, you cannot pass an `Outer` to it even if `Outer` implements `Deref`. ## Signature ```rust theme={null} pub trait Deref ``` ## Examples ```rust theme={null} struct Wrapper { inner: T } impl WrapperDeref of Deref> { type Target = T; fn deref(self: Wrapper) -> T { self.inner } } let wrapped = Wrapper { inner: 42 }; assert!(wrapped.deref() == 42); ``` ## Trait functions ### deref Returns the dereferenced value. #### Signature ```rust theme={null} fn deref(self: T) -> DerefTarget ``` ## Trait types ### Target The type of the dereferenced value. #### Signature ```rust theme={null} type Target; ``` # core::ops::deref::DerefMut Source: https://docs.starknet.io/build/corelib/core-ops-deref-DerefMut A trait for dereferencing in mutable contexts. This trait is similar to `Deref` but specifically handles cases where the value accessed is mutable. Despite its name, `DerefMut` does NOT allow modifying the inner value - it only indicates that the container itself is mutable. ## Signature ```rust theme={null} pub trait DerefMut ``` ## Examples ```rust theme={null} #[derive(Copy, Drop)] struct MutWrapper { value: T } impl MutWrapperDerefMut> of DerefMut> { type Target = T; fn deref_mut(ref self: MutWrapper) -> T { self.value } } // This will work since x is mutable let mut x = MutWrapper { value: 42 }; let val = x.deref_mut(); assert!(val == 42); // This would fail to compile since y is not mutable let y = MutWrapper { value: 42 }; let val = y.deref_mut(); // Compile error ``` ## Trait functions ### deref\_mut Returns the dereferenced value. #### Signature ```rust theme={null} fn deref_mut(ref self: T) -> DerefMutTarget ``` ## Trait types ### Target The type of the dereferenced value. #### Signature ```rust theme={null} type Target; ``` # core::ops::function::Fn Source: https://docs.starknet.io/build/corelib/core-ops-function-Fn The version of the call operator that takes a by-snapshot receiver. Instances of `Fn` can be called repeatedly. `Fn` is implemented automatically by closures whose captured variables are all `Copy`. Additionally, for any type `F` that implements `Fn`, `@F` implements `Fn`, too. Since [`FnOnce`](./core-ops-function-FnOnce) is implemented for all implementers of `Fn`, any instance of `Fn` can be used as a parameter where a [`FnOnce`](./core-ops-function-FnOnce) is expected. Use `Fn` as a bound when you want to accept a parameter of function-like type and need to call it repeatedly. If you do not need such strict requirements, use [`FnOnce`](./core-ops-function-FnOnce) as bounds. ## Signature ```rust theme={null} pub trait Fn ``` ## Examples ## Calling a closure ```rust theme={null} let square = |x| x * x; assert_eq!(square(5), 25); ``` ## Using a `Fn` parameter ```rust theme={null} fn call_with_one, +core::ops::Fn[Output: usize]>(func: F) -> usize { func(1) } let double = |x| x * 2; assert_eq!(call_with_one(double), 2); ``` ## Trait functions ### call Performs the call operation. #### Signature ```rust theme={null} fn call(self: @T, args: Args) -> FnOutput ``` ## Trait types ### Output The returned type after the call operator is used. #### Signature ```rust theme={null} type Output; ``` # core::ops::function::FnOnce Source: https://docs.starknet.io/build/corelib/core-ops-function-FnOnce The version of the call operator that takes a by-value receiver. Instances of `FnOnce` can be called, but might not be callable multiple times. Because of this, if the only thing known about a type is that it implements `FnOnce`, it can only be called once. `FnOnce` is implemented automatically by closures that might consume captured variables. ## Signature ```rust theme={null} pub trait FnOnce ``` ## Examples ```rust theme={null} fn consume_with_relish, +core::ops::FnOnce[Output: O], +core::fmt::Display, +Drop, >( func: F, ) { // `func` consumes its captured variables, so it cannot be run more // than once. println!("Consumed: {}", func()); println!("Delicious!"); // Attempting to invoke `func()` again will throw a `Variable was previously moved.` // error for `func`. } let x: ByteArray = "x"; let consume_and_return_x = || x; consume_with_relish(consume_and_return_x); // `consume_and_return_x` can no longer be invoked at this point ``` ## Trait functions ### call Performs the call operation. #### Signature ```rust theme={null} fn call(self: T, args: Args) -> FnOnceOutput ``` ## Trait types ### Output The returned type after the call operator is used. #### Signature ```rust theme={null} type Output; ``` # core::ops::index::Index Source: https://docs.starknet.io/build/corelib/core-ops-index-Index A trait for indexing operations (`container[index]`) where the input type is mutated. This trait should be implemented when you want to implement indexing operations on a type that's mutated by a read access. This is useful for any type depending on a [`Felt252Dict`](./core-dict-Felt252Dict), where dictionary accesses are modifying the data structure itself. `container[index]` is syntactic sugar for `container.index(index)`. ## Signature ```rust theme={null} pub trait Index ``` ## Examples The following example implements `Index` on a `Stack` type. This `Stack` is implemented based on a [`Felt252Dict`](./core-dict-Felt252Dict), where dictionary accesses are modifying the dictionary itself. As such, we must implement the `Index` trait instead of the `IndexView` trait. ```rust theme={null} use core::ops::Index; #[derive(Destruct, Default)] struct Stack { items: Felt252Dict, len: usize } #[generate_trait] impl StackImpl of StackTrait { fn push(ref self: Stack, item: u128) { self.items.insert(self.len.into(), item); self.len += 1; } } impl StackIndex of Index { type Target = u128; fn index(ref self: Stack, index: usize) -> Self::Target { if index >= self.len { panic!("Index out of bounds"); } self.items.get(index.into()) } } let mut stack: Stack = Default::default(); stack.push(1); assert!(stack[0] == 1); ``` ## Trait functions ### index Performs the indexing (`container[index]`) operation. #### Panics May panic if the index is out of bounds. #### Signature ```rust theme={null} fn index(ref self: C, index: I) -> IndexTarget ``` ## Trait types ### Target The returned type after indexing. #### Signature ```rust theme={null} type Target; ``` # core::ops::index::IndexView Source: https://docs.starknet.io/build/corelib/core-ops-index-IndexView A trait for indexing operations (`container[index]`) where the input type is not modified. `container[index]` is syntactic sugar for `container.index(index)`. ## Signature ```rust theme={null} pub trait IndexView ``` ## Examples The following example implements `IndexView` on a `NucleotideCount` container, which can be indexed without modifying the input, enabling individual counts to be retrieved with index syntax. ```rust theme={null} use core::ops::IndexView; #[derive(Copy, Drop)] enum Nucleotide { A, C, G, T, } #[derive(Copy, Drop)] struct NucleotideCount { a: usize, c: usize, g: usize, t: usize, } impl NucleotideIndex of IndexView { type Target = usize; fn index(self: @NucleotideCount, index: Nucleotide) -> Self::Target { match index { Nucleotide::A => *self.a, Nucleotide::C => *self.c, Nucleotide::G => *self.g, Nucleotide::T => *self.t, } } } let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12}; assert!(nucleotide_count[Nucleotide::A] == 14); assert!(nucleotide_count[Nucleotide::C] == 9); assert!(nucleotide_count[Nucleotide::G] == 10); assert!(nucleotide_count[Nucleotide::T] == 12); ``` ## Trait functions ### index Performs the indexing (`container[index]`) operation. #### Panics May panic if the index is out of bounds. #### Signature ```rust theme={null} fn index(self: @C, index: I) -> IndexViewTarget ``` ## Trait types ### Target The returned type after indexing. #### Signature ```rust theme={null} type Target; ``` # core::ops::range::Range Source: https://docs.starknet.io/build/corelib/core-ops-range-Range A (half-open) range bounded inclusively below and exclusively above (`start..end`). The range `start..end` contains all values with `start = end`. ## Signature ```rust theme={null} #[derive(Clone, Drop, PartialEq)] pub struct Range { pub start: T, pub end: T, } ``` ## Examples The `start..end` syntax is a `Range`: ```rust theme={null} assert!((3..5) == core::ops::Range { start: 3, end: 5 }); let mut sum = 0; for i in 3..6 { sum += i; } assert!(sum == 3 + 4 + 5); ``` ## Members ### start The lower bound of the range (inclusive). #### Signature ```rust theme={null} pub start: T ``` ### end The upper bound of the range (exclusive). #### Signature ```rust theme={null} pub end: T ``` # core::ops::range::RangeInclusive Source: https://docs.starknet.io/build/corelib/core-ops-range-RangeInclusive Represents the range start, end. ## Signature ```rust theme={null} #[derive(Clone, Drop, PartialEq)] pub struct RangeInclusive { pub start: T, pub end: T, } ``` ## Members ### start The
프로젝트로 돌아가기