Trait nostr_sdk::alloc::str::FromStr

1.0.0 · source ·
pub trait FromStr: Sized {
    type Err;

    // Required method
    fn from_str(s: &str) -> Result<Self, Self::Err>;
}
Expand description

Parse a value from a string

FromStr’s from_str method is often used implicitly, through str’s parse method. See parse’s documentation for examples.

FromStr does not have a lifetime parameter, and so you can only parse types that do not contain a lifetime parameter themselves. In other words, you can parse an i32 with FromStr, but not a &i32. You can parse a struct that contains an i32, but not one that contains an &i32.

Examples

Basic implementation of FromStr on an example Point type:

use std::str::FromStr;

#[derive(Debug, PartialEq)]
struct Point {
    x: i32,
    y: i32
}

#[derive(Debug, PartialEq, Eq)]
struct ParsePointError;

impl FromStr for Point {
    type Err = ParsePointError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (x, y) = s
            .strip_prefix('(')
            .and_then(|s| s.strip_suffix(')'))
            .and_then(|s| s.split_once(','))
            .ok_or(ParsePointError)?;

        let x_fromstr = x.parse::<i32>().map_err(|_| ParsePointError)?;
        let y_fromstr = y.parse::<i32>().map_err(|_| ParsePointError)?;

        Ok(Point { x: x_fromstr, y: y_fromstr })
    }
}

let expected = Ok(Point { x: 1, y: 2 });
// Explicit call
assert_eq!(Point::from_str("(1,2)"), expected);
// Implicit calls, through parse
assert_eq!("(1,2)".parse(), expected);
assert_eq!("(1,2)".parse::<Point>(), expected);
// Invalid input string
assert!(Point::from_str("(1 2)").is_err());

Required Associated Types§

source

type Err

The associated error which can be returned from parsing.

Required Methods§

source

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type.

If parsing succeeds, return the value inside Ok, otherwise when the string is ill-formatted return an error specific to the inside Err. The error type is specific to the implementation of the trait.

Examples

Basic usage with i32, a type that implements FromStr:

use std::str::FromStr;

let s = "5";
let x = i32::from_str(s).unwrap();

assert_eq!(5, x);

Implementors§

§

impl FromStr for WitnessVersion

§

type Err = Error

§

impl FromStr for ChildNumber

§

type Err = Error

§

impl FromStr for LockTime

§

impl FromStr for AddressType

§

type Err = Error

§

impl FromStr for Denomination

§

impl FromStr for Network

§

impl FromStr for EcdsaSighashType

§

impl FromStr for TapSighashType

source§

impl FromStr for Alphabet

source§

impl FromStr for HttpMethod

§

type Err = Error

source§

impl FromStr for Kind

source§

impl FromStr for RelayMetadata

§

type Err = Error

source§

impl FromStr for Report

§

type Err = Error

source§

impl FromStr for LiveEventMarker

§

type Err = Error

source§

impl FromStr for Condition

§

type Err = Error

1.7.0 · source§

impl FromStr for IpAddr

source§

impl FromStr for SocketAddr

source§

impl FromStr for Value

§

type Err = Error

source§

impl FromStr for IpNet

source§

impl FromStr for log::Level

source§

impl FromStr for log::LevelFilter

source§

impl FromStr for bool

1.20.0 · source§

impl FromStr for char

source§

impl FromStr for f32

source§

impl FromStr for f64

source§

impl FromStr for i8

source§

impl FromStr for i16

source§

impl FromStr for i32

source§

impl FromStr for i64

source§

impl FromStr for i128

source§

impl FromStr for isize

source§

impl FromStr for u8

source§

impl FromStr for u16

source§

impl FromStr for u32

source§

impl FromStr for u64

source§

impl FromStr for u128

source§

impl FromStr for usize

§

impl FromStr for Mnemonic

§

type Err = Error

§

impl FromStr for ChainCode

§

type Err = Error

§

impl FromStr for DerivationPath

§

type Err = Error

§

impl FromStr for ExtendedPrivKey

§

type Err = Error

§

impl FromStr for ExtendedPubKey

§

type Err = Error

§

impl FromStr for Fingerprint

§

type Err = Error

§

impl FromStr for ShortId

§

type Err = Error

§

impl FromStr for ChainHash

§

type Err = Error

§

impl FromStr for nostr_sdk::bitcoin::blockdata::locktime::absolute::Height

§

type Err = Error

§

impl FromStr for nostr_sdk::bitcoin::blockdata::locktime::absolute::Time

§

type Err = Error

§

impl FromStr for nostr_sdk::bitcoin::blockdata::locktime::relative::Height

§

impl FromStr for nostr_sdk::bitcoin::blockdata::locktime::relative::Time

§

impl FromStr for nostr_sdk::bitcoin::ecdsa::Signature

§

type Err = Error

§

impl FromStr for FilterHash

§

type Err = Error

§

impl FromStr for FilterHeader

§

type Err = Error

§

impl FromStr for TxMerkleNode

§

type Err = Error

§

impl FromStr for WitnessCommitment

§

type Err = Error

§

impl FromStr for WitnessMerkleNode

§

type Err = Error

§

impl FromStr for XpubIdentifier

§

type Err = Error

§

impl FromStr for KeyPair

§

type Err = Error

§

impl FromStr for XOnlyPublicKey

§

type Err = Error

§

impl FromStr for CommandString

§

impl FromStr for Magic

§

impl FromStr for PsbtSighashType

§

impl FromStr for LegacySighash

§

type Err = Error

§

impl FromStr for SegwitV0Sighash

§

type Err = Error

§

impl FromStr for TapSighash

§

type Err = Error

§

impl FromStr for Address<NetworkUnchecked>

Address can be parsed only with NetworkUnchecked.

§

type Err = Error

§

impl FromStr for Amount

§

impl FromStr for BlockHash

§

type Err = Error

§

impl FromStr for FeeRate

§

impl FromStr for OutPoint

§

impl FromStr for PrivateKey

§

type Err = Error

§

impl FromStr for PubkeyHash

§

type Err = Error

§

impl FromStr for nostr_sdk::bitcoin::PublicKey

§

type Err = Error

§

impl FromStr for ScriptHash

§

type Err = Error

§

impl FromStr for Sequence

§

impl FromStr for SignedAmount

§

impl FromStr for Txid

§

type Err = Error

§

impl FromStr for WPubkeyHash

§

type Err = Error

§

impl FromStr for WScriptHash

§

type Err = Error

§

impl FromStr for Weight

§

impl FromStr for Wtxid

§

type Err = Error

§

impl FromStr for TapLeafHash

§

type Err = Error

§

impl FromStr for TapNodeHash

§

type Err = Error

§

impl FromStr for TapTweakHash

§

type Err = Error

§

impl FromStr for SharedSecret

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::ecdsa::Signature

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::hashes::hash160::Hash

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::hashes::ripemd160::Hash

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::hashes::sha1::Hash

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::hashes::sha256::Hash

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::hashes::sha256::Midstate

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::hashes::sha256d::Hash

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::hashes::sha512::Hash

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::hashes::sha512_256::Hash

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::hashes::siphash24::Hash

§

type Err = Error

§

impl FromStr for nostr_sdk::key::secp256k1::schnorr::Signature

§

type Err = Error

§

impl FromStr for nostr_sdk::key::PublicKey

§

type Err = Error

§

impl FromStr for SecretKey

§

type Err = Error

source§

impl FromStr for Conditions

§

type Err = Error

source§

impl FromStr for DelegationTag

§

type Err = Error

source§

impl FromStr for NostrConnectURI

§

type Err = Error

source§

impl FromStr for NostrWalletConnectURI

§

type Err = Error

source§

impl FromStr for Ipv4Addr

source§

impl FromStr for Ipv6Addr

1.5.0 · source§

impl FromStr for SocketAddrV4

1.5.0 · source§

impl FromStr for SocketAddrV6

1.35.0 · source§

impl FromStr for NonZeroI8

1.35.0 · source§

impl FromStr for NonZeroI16

1.35.0 · source§

impl FromStr for NonZeroI32

1.35.0 · source§

impl FromStr for NonZeroI64

1.35.0 · source§

impl FromStr for NonZeroI128

1.35.0 · source§

impl FromStr for NonZeroIsize

1.35.0 · source§

impl FromStr for NonZeroU8

1.35.0 · source§

impl FromStr for NonZeroU16

1.35.0 · source§

impl FromStr for NonZeroU32

1.35.0 · source§

impl FromStr for NonZeroU64

1.35.0 · source§

impl FromStr for NonZeroU128

1.35.0 · source§

impl FromStr for NonZeroUsize

source§

impl FromStr for Number

§

type Err = Error

source§

impl FromStr for EventId

§

type Err = Error

source§

impl FromStr for ImageDimensions

§

type Err = Error

source§

impl FromStr for Timestamp

source§

impl FromStr for UncheckedUrl

§

type Err = Error

§

impl FromStr for nostr_sdk::Url

Parse a string as an URL, without a base URL or encoding override.

source§

impl FromStr for String

1.45.0 · source§

impl FromStr for OsString

1.32.0 · source§

impl FromStr for PathBuf

source§

impl FromStr for HeaderName

source§

impl FromStr for HeaderValue

source§

impl FromStr for Method

source§

impl FromStr for StatusCode

source§

impl FromStr for Authority

source§

impl FromStr for PathAndQuery

source§

impl FromStr for Scheme

source§

impl FromStr for Uri

source§

impl FromStr for Ipv4Net

source§

impl FromStr for Ipv6Net

source§

impl FromStr for Mime

source§

impl FromStr for url::Url

Parse a string as an URL, without a base URL or encoding override.

§

impl FromStr for Hash

§

type Err = Error

§

impl FromStr for Hash

§

type Err = Error

§

impl FromStr for Hash

§

type Err = Error

§

impl FromStr for Hash

§

type Err = Error

§

impl FromStr for Hash

§

type Err = Error

§

impl FromStr for Hash

§

type Err = Error

§

impl FromStr for Hash

§

type Err = Error

§

impl FromStr for Header

§

type Err = Error

§

impl FromStr for Level

§

type Err = ParseLevelError

§

impl FromStr for LevelFilter

§

type Err = ParseLevelFilterError

§

impl FromStr for Midstate

§

type Err = Error

§

impl FromStr for Name

§

type Err = InvalidNameError

§

impl FromStr for Response

§

type Err = Error

§

impl<T> FromStr for nostr_sdk::key::secp256k1::hashes::sha256t::Hash<T>where T: Tag,

§

type Err = Error

§

impl<T> FromStr for nostr_sdk::key::secp256k1::hashes::Hmac<T>where T: Hash + FromStr,

§

type Err = <T as FromStr>::Err

§

impl<T> FromStr for Hash<T>where T: Tag,

§

type Err = Error

§

impl<T> FromStr for Hmac<T>where T: Hash + FromStr,

§

type Err = <T as FromStr>::Err