Skip to main content

TryIntoAccountId

Trait TryIntoAccountId 

Source
pub trait TryIntoAccountId {
    // Required methods
    fn try_into_account_id(self) -> Result<AccountId, ParseAccountError>;
    fn as_str(&self) -> &str;
}
Expand description

A trait for types that can be converted into an AccountId

This trait allows functions to accept account IDs in multiple forms without requiring explicit cloning or type conversions from the caller.

When writing functions that accept account IDs, you often want to support multiple input types for convenience:

  • Owned AccountId - when the caller already has ownership
  • &AccountId - when the caller wants to keep their copy
  • &AccountIdRef - when working with borrowed account ID references
  • &str - when you have string slice that needs validation
  • Owned String - when you have and owned string that needs validation

Without this trait, you’d need either:

  • Multiple function overloads
  • Explicit .clone() calls from users
  • A single restrictive type that’s inconvenient

§Examples

use near_account_id::{AccountId, AccountIdRef, TryIntoAccountId};

fn process_account(account: impl TryIntoAccountId) -> Result<(), Box<dyn std::error::Error>> {
    let account_id: AccountId = account.try_into_account_id()?;
    println!("Processing {}", account_id);
    // Use account_id...
    Ok(())
}

// All of these work:
let owned: AccountId = "alice.near".parse().unwrap();
process_account(owned)?; // Moves ownership, no clone

let owned: AccountId = "bob.near".parse().unwrap();
process_account(&owned)?; // Clones internally, caller keeps ownership

let borrowed: &AccountIdRef = AccountIdRef::new("carol.near").unwrap();
process_account(borrowed)?; // Clones internally

process_account("impostor.near")?; // Validates the string
process_account("other.near".to_string())?; // Validates and consumes

§Errors

Returns ParseAccountError when the input string is not a valid NEAR account ID. This can happen when:

  • The string is too short (< 2 characters) or too long (> 64 characters)
  • The string contains invalid characters (only a-z, 0-9, -, _, . are allowed)
  • The string has redundant separators (e.g., .., --, __, or starts/ends with separators)

Note that validated types (AccountId, &AccountId, &AccountIdRef) will never return an error since they’ve already passed validation.

Required Methods§

Source

fn try_into_account_id(self) -> Result<AccountId, ParseAccountError>

Converts this type into an owned AccountId.

For already-validated types (AccountId, &AccountId, &AccountIdRef), this moves or clones the value without re-validation.

For string types (&str, String), this validates the format and returns an error if invalid.

§Errors

Returns ParseAccountError if the input string is not a valid NEAR account ID.

Source

fn as_str(&self) -> &str

Implementations on Foreign Types§

Source§

impl TryIntoAccountId for &str

Source§

impl TryIntoAccountId for &String

Source§

impl TryIntoAccountId for Cow<'_, AccountIdRef>

Source§

impl TryIntoAccountId for String

Implementors§