pub struct AccountId(/* private fields */);Expand description
NEAR Account Identifier.
This is a unique, syntactically valid, human-readable account identifier on the NEAR network.
See the crate-level docs for information about validation.
Also see Error kind precedence.
§Examples
use near_account_id::AccountId;
let alice: AccountId = "alice.near".parse().unwrap();
assert!("ƒelicia.near".parse::<AccountId>().is_err()); // (ƒ is not f)Implementations§
Source§impl AccountId
impl AccountId
Sourcepub fn validate(account_id: &str) -> Result<(), ParseAccountError>
pub fn validate(account_id: &str) -> Result<(), ParseAccountError>
Validates a string as a well-structured NEAR Account ID.
Checks Account ID validity without constructing an AccountId instance.
§Examples
use near_account_id::{AccountId, ParseErrorKind};
assert!(AccountId::validate("alice.near").is_ok());
assert!(
matches!(
AccountId::validate("ƒelicia.near"), // fancy ƒ!
Err(err) if err.kind() == &ParseErrorKind::InvalidChar
)
);§Error kind precedence
If an Account ID has multiple format violations, the first one would be reported.
§Examples
use near_account_id::{AccountId, ParseErrorKind};
assert!(
matches!(
AccountId::validate("A__ƒƒluent."),
Err(err) if err.kind() == &ParseErrorKind::InvalidChar
)
);
assert!(
matches!(
AccountId::validate("a__ƒƒluent."),
Err(err) if err.kind() == &ParseErrorKind::RedundantSeparator
)
);
assert!(
matches!(
AccountId::validate("aƒƒluent."),
Err(err) if err.kind() == &ParseErrorKind::InvalidChar
)
);
assert!(
matches!(
AccountId::validate("affluent."),
Err(err) if err.kind() == &ParseErrorKind::RedundantSeparator
)
);Methods from Deref<Target = AccountIdRef>§
pub const MIN_LEN: usize = 2usize
pub const MAX_LEN: usize = 64usize
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns a string slice of the entire Account ID.
§Examples
use near_account_id::AccountIdRef;
let carol = AccountIdRef::new("carol.near").unwrap();
assert_eq!("carol.near", carol.as_str());Sourcepub fn is_top_level(&self) -> bool
pub fn is_top_level(&self) -> bool
Returns true if the account ID is a top-level NEAR Account ID.
See Top-level Accounts.
§Examples
use near_account_id::AccountIdRef;
let near_tla = AccountIdRef::new("near").unwrap();
assert!(near_tla.is_top_level());
// "alice.near" is a sub account of "near" account
let alice = AccountIdRef::new("alice.near").unwrap();
assert!(!alice.is_top_level());Sourcepub fn is_sub_account_of(&self, parent: &AccountIdRef) -> bool
pub fn is_sub_account_of(&self, parent: &AccountIdRef) -> bool
Returns true if the AccountId is a direct sub-account of the provided parent account.
See Subaccounts.
§Examples
use near_account_id::AccountId;
let near_tla: AccountId = "near".parse().unwrap();
assert!(near_tla.is_top_level());
let alice: AccountId = "alice.near".parse().unwrap();
assert!(alice.is_sub_account_of(&near_tla));
let alice_app: AccountId = "app.alice.near".parse().unwrap();
// While app.alice.near is a sub account of alice.near,
// app.alice.near is not a sub account of near
assert!(alice_app.is_sub_account_of(&alice));
assert!(!alice_app.is_sub_account_of(&near_tla));Sourcepub fn get_account_type(&self) -> AccountType
pub fn get_account_type(&self) -> AccountType
Returns AccountType::EthImplicitAccount if the AccountId is a 40 characters long hexadecimal prefixed with ‘0x’.
Returns AccountType::NearImplicitAccount if the AccountId is a 64 characters long hexadecimal.
Otherwise, returns AccountType::NamedAccount.
See Implicit-Accounts.
§Examples
use near_account_id::{AccountId, AccountType};
let alice: AccountId = "alice.near".parse().unwrap();
assert!(alice.get_account_type() == AccountType::NamedAccount);
let eth_rando = "0xb794f5ea0ba39494ce839613fffba74279579268"
.parse::<AccountId>()
.unwrap();
assert!(eth_rando.get_account_type() == AccountType::EthImplicitAccount);
let near_rando = "98793cd91a3f870fb126f66285808c7e094afcfc4eda8a970f6648cdf0dbd6de"
.parse::<AccountId>()
.unwrap();
assert!(near_rando.get_account_type() == AccountType::NearImplicitAccount);Sourcepub fn is_system(&self) -> bool
pub fn is_system(&self) -> bool
Returns true if this AccountId is the system account.
See System account.
§Examples
use near_account_id::AccountId;
let alice: AccountId = "alice.near".parse().unwrap();
assert!(!alice.is_system());
let system: AccountId = "system".parse().unwrap();
assert!(system.is_system());Sourcepub fn get_parent_account_id(&self) -> Option<&AccountIdRef>
pub fn get_parent_account_id(&self) -> Option<&AccountIdRef>
Returns parent’s account id reference
§Examples
use near_account_id::AccountIdRef;
let alice: &AccountIdRef = AccountIdRef::new_or_panic("alice.near");
let parent: &AccountIdRef = alice.get_parent_account_id().unwrap();
assert!(alice.is_sub_account_of(parent));
let near: &AccountIdRef = AccountIdRef::new_or_panic("near");
assert!(near.get_parent_account_id().is_none());
let implicit: &AccountIdRef = AccountIdRef::new_or_panic("248e104d1d4764d713c4211c13808c8fc887869c580f4178e60538ac5c2a0b26");
assert!(implicit.get_parent_account_id().is_none());Trait Implementations§
Source§impl AsRef<AccountIdRef> for AccountId
impl AsRef<AccountIdRef> for AccountId
Source§fn as_ref(&self) -> &AccountIdRef
fn as_ref(&self) -> &AccountIdRef
Source§impl Borrow<AccountIdRef> for AccountId
impl Borrow<AccountIdRef> for AccountId
Source§fn borrow(&self) -> &AccountIdRef
fn borrow(&self) -> &AccountIdRef
Source§impl BorshDeserialize for AccountId
impl BorshDeserialize for AccountId
fn deserialize_reader<R>(rd: &mut R) -> Result<AccountId, Error>where
R: Read,
Source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
Source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where
R: Read,
Source§impl BorshSerialize for AccountId
impl BorshSerialize for AccountId
Source§impl<'de> Deserialize<'de> for AccountId
impl<'de> Deserialize<'de> for AccountId
Source§fn deserialize<D>(
deserializer: D,
) -> Result<AccountId, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<AccountId, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<'a> From<&'a AccountIdRef> for AccountId
impl<'a> From<&'a AccountIdRef> for AccountId
Source§fn from(id: &'a AccountIdRef) -> AccountId
fn from(id: &'a AccountIdRef) -> AccountId
Source§impl Ord for AccountId
impl Ord for AccountId
Source§impl<'a> PartialEq<&'a AccountIdRef> for AccountId
impl<'a> PartialEq<&'a AccountIdRef> for AccountId
Source§impl<'a> PartialEq<AccountId> for &'a AccountIdRef
impl<'a> PartialEq<AccountId> for &'a AccountIdRef
Source§impl PartialEq<AccountId> for AccountIdRef
impl PartialEq<AccountId> for AccountIdRef
Source§impl PartialEq<AccountIdRef> for AccountId
impl PartialEq<AccountIdRef> for AccountId
Source§impl<'a> PartialOrd<&'a AccountIdRef> for AccountId
impl<'a> PartialOrd<&'a AccountIdRef> for AccountId
Source§impl<'a> PartialOrd<&'a str> for AccountId
impl<'a> PartialOrd<&'a str> for AccountId
Source§impl<'a> PartialOrd<AccountId> for &'a AccountIdRef
impl<'a> PartialOrd<AccountId> for &'a AccountIdRef
Source§impl<'a> PartialOrd<AccountId> for &'a str
impl<'a> PartialOrd<AccountId> for &'a str
Source§impl PartialOrd<AccountId> for AccountIdRef
impl PartialOrd<AccountId> for AccountIdRef
Source§impl PartialOrd<AccountId> for str
impl PartialOrd<AccountId> for str
Source§impl PartialOrd<AccountIdRef> for AccountId
impl PartialOrd<AccountIdRef> for AccountId
Source§impl PartialOrd<String> for AccountId
impl PartialOrd<String> for AccountId
Source§impl PartialOrd<str> for AccountId
impl PartialOrd<str> for AccountId
Source§impl PartialOrd for AccountId
impl PartialOrd for AccountId
Source§impl Serialize for AccountId
impl Serialize for AccountId
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl Eq for AccountId
impl StructuralPartialEq for AccountId
Auto Trait Implementations§
impl Freeze for AccountId
impl RefUnwindSafe for AccountId
impl Send for AccountId
impl Sync for AccountId
impl Unpin for AccountId
impl UnwindSafe for AccountId
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.