Trait floresta_common::prelude::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§

source§

impl FromStr for AddressType

source§

impl FromStr for Denomination

source§

impl FromStr for ChildNumber

§

type Err = Error

source§

impl FromStr for LockTime

source§

impl FromStr for WitnessVersion

source§

impl FromStr for EcdsaSighashType

source§

impl FromStr for TapSighashType

source§

impl FromStr for Network

source§

impl FromStr for DescriptorPublicKey

source§

impl FromStr for DescriptorSecretKey

1.7.0 · source§

impl FromStr for IpAddr

source§

impl FromStr for SocketAddr

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

source§

impl FromStr for Address<NetworkUnchecked>

Address can be parsed only with NetworkUnchecked.

source§

impl FromStr for Amount

source§

impl FromStr for SignedAmount

source§

impl FromStr for ChainCode

source§

impl FromStr for DerivationPath

§

type Err = Error

source§

impl FromStr for Fingerprint

source§

impl FromStr for XKeyIdentifier

source§

impl FromStr for Xpriv

§

type Err = Error

source§

impl FromStr for Xpub

§

type Err = Error

source§

impl FromStr for XpubIdentifier

source§

impl FromStr for ShortId

source§

impl FromStr for ChainHash

source§

impl FromStr for FeeRate

source§

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

§

type Err = Error

source§

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

§

type Err = Error

source§

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

source§

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

source§

impl FromStr for ScriptHash

source§

impl FromStr for WScriptHash

source§

impl FromStr for OutPoint

source§

impl FromStr for Sequence

source§

impl FromStr for Weight

source§

impl FromStr for bitcoin::crypto::ecdsa::Signature

§

type Err = Error

source§

impl FromStr for PrivateKey

§

type Err = Error

source§

impl FromStr for PubkeyHash

source§

impl FromStr for bitcoin::crypto::key::PublicKey

§

type Err = Error

source§

impl FromStr for WPubkeyHash

source§

impl FromStr for LegacySighash

source§

impl FromStr for SegwitV0Sighash

source§

impl FromStr for TapSighash

source§

impl FromStr for BlockHash

source§

impl FromStr for FilterHash

source§

impl FromStr for FilterHeader

source§

impl FromStr for TxMerkleNode

source§

impl FromStr for Txid

source§

impl FromStr for WitnessCommitment

source§

impl FromStr for WitnessMerkleNode

source§

impl FromStr for Wtxid

source§

impl FromStr for CommandString

source§

impl FromStr for Magic

source§

impl FromStr for PsbtSighashType

source§

impl FromStr for TapLeafHash

source§

impl FromStr for TapNodeHash

source§

impl FromStr for TapTweakHash

source§

impl FromStr for bitcoin_hashes::hash160::Hash

source§

impl FromStr for bitcoin_hashes::ripemd160::Hash

source§

impl FromStr for bitcoin_hashes::sha1::Hash

source§

impl FromStr for bitcoin_hashes::sha256::Hash

source§

impl FromStr for Midstate

source§

impl FromStr for bitcoin_hashes::sha256d::Hash

source§

impl FromStr for bitcoin_hashes::sha512::Hash

source§

impl FromStr for bitcoin_hashes::sha512_256::Hash

source§

impl FromStr for bitcoin_hashes::siphash24::Hash

source§

impl FromStr for DefiniteDescriptorKey

source§

impl FromStr for miniscript::miniscript::hash256::Hash

source§

impl FromStr for SharedSecret

§

type Err = Error

source§

impl FromStr for secp256k1::ecdsa::Signature

§

type Err = Error

source§

impl FromStr for ElligatorSwift

§

type Err = Error

source§

impl FromStr for Keypair

§

type Err = Error

source§

impl FromStr for secp256k1::key::PublicKey

§

type Err = Error

source§

impl FromStr for SecretKey

§

type Err = Error

source§

impl FromStr for XOnlyPublicKey

§

type Err = Error

source§

impl FromStr for secp256k1::schnorr::Signature

§

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

1.45.0 · source§

impl FromStr for OsString

1.32.0 · source§

impl FromStr for PathBuf

source§

impl FromStr for String

source§

impl<Pk> FromStr for Descriptor<Pk>where Pk: MiniscriptKey + FromStr, <Pk as MiniscriptKey>::Sha256: FromStr, <Pk as MiniscriptKey>::Hash256: FromStr, <Pk as MiniscriptKey>::Ripemd160: FromStr, <Pk as MiniscriptKey>::Hash160: FromStr, <Pk as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Sha256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Ripemd160 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash160 as FromStr>::Err: ToString,

§

type Err = Error

source§

impl<Pk> FromStr for miniscript::policy::concrete::Policy<Pk>where Pk: MiniscriptKey + FromStr, <Pk as MiniscriptKey>::Sha256: FromStr, <Pk as MiniscriptKey>::Hash256: FromStr, <Pk as MiniscriptKey>::Ripemd160: FromStr, <Pk as MiniscriptKey>::Hash160: FromStr, <Pk as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Sha256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Ripemd160 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash160 as FromStr>::Err: ToString,

§

type Err = Error

source§

impl<Pk> FromStr for miniscript::policy::semantic::Policy<Pk>where Pk: MiniscriptKey + FromStr, <Pk as MiniscriptKey>::Sha256: FromStr, <Pk as MiniscriptKey>::Hash256: FromStr, <Pk as MiniscriptKey>::Ripemd160: FromStr, <Pk as MiniscriptKey>::Hash160: FromStr, <Pk as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Sha256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Ripemd160 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash160 as FromStr>::Err: ToString,

§

type Err = Error

source§

impl<Pk> FromStr for Bare<Pk>where Pk: MiniscriptKey + FromStr, <Pk as MiniscriptKey>::Sha256: FromStr, <Pk as MiniscriptKey>::Hash256: FromStr, <Pk as MiniscriptKey>::Ripemd160: FromStr, <Pk as MiniscriptKey>::Hash160: FromStr, <Pk as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Sha256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Ripemd160 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash160 as FromStr>::Err: ToString,

§

type Err = Error

source§

impl<Pk> FromStr for Pkh<Pk>where Pk: MiniscriptKey + FromStr, <Pk as MiniscriptKey>::Sha256: FromStr, <Pk as MiniscriptKey>::Hash256: FromStr, <Pk as MiniscriptKey>::Ripemd160: FromStr, <Pk as MiniscriptKey>::Hash160: FromStr, <Pk as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Sha256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Ripemd160 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash160 as FromStr>::Err: ToString,

§

type Err = Error

source§

impl<Pk> FromStr for Wpkh<Pk>where Pk: MiniscriptKey + FromStr, <Pk as MiniscriptKey>::Sha256: FromStr, <Pk as MiniscriptKey>::Hash256: FromStr, <Pk as MiniscriptKey>::Ripemd160: FromStr, <Pk as MiniscriptKey>::Hash160: FromStr, <Pk as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Sha256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Ripemd160 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash160 as FromStr>::Err: ToString,

§

type Err = Error

source§

impl<Pk> FromStr for Wsh<Pk>where Pk: MiniscriptKey + FromStr, <Pk as MiniscriptKey>::Sha256: FromStr, <Pk as MiniscriptKey>::Hash256: FromStr, <Pk as MiniscriptKey>::Ripemd160: FromStr, <Pk as MiniscriptKey>::Hash160: FromStr, <Pk as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Sha256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Ripemd160 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash160 as FromStr>::Err: ToString,

§

type Err = Error

source§

impl<Pk> FromStr for Sh<Pk>where Pk: MiniscriptKey + FromStr, <Pk as MiniscriptKey>::Sha256: FromStr, <Pk as MiniscriptKey>::Hash256: FromStr, <Pk as MiniscriptKey>::Ripemd160: FromStr, <Pk as MiniscriptKey>::Hash160: FromStr, <Pk as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Sha256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Ripemd160 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash160 as FromStr>::Err: ToString,

§

type Err = Error

source§

impl<Pk> FromStr for Tr<Pk>where Pk: MiniscriptKey + FromStr, <Pk as MiniscriptKey>::Sha256: FromStr, <Pk as MiniscriptKey>::Hash256: FromStr, <Pk as MiniscriptKey>::Ripemd160: FromStr, <Pk as MiniscriptKey>::Hash160: FromStr, <Pk as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Sha256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Ripemd160 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash160 as FromStr>::Err: ToString,

§

type Err = Error

source§

impl<Pk, Ctx> FromStr for Miniscript<Pk, Ctx>where Pk: MiniscriptKey + FromStr, <Pk as MiniscriptKey>::Sha256: FromStr, <Pk as MiniscriptKey>::Hash256: FromStr, <Pk as MiniscriptKey>::Ripemd160: FromStr, <Pk as MiniscriptKey>::Hash160: FromStr, <Pk as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Sha256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash256 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Ripemd160 as FromStr>::Err: ToString, <<Pk as MiniscriptKey>::Hash160 as FromStr>::Err: ToString, Ctx: ScriptContext,

§

type Err = Error

source§

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

§

type Err = <T as FromStr>::Err

source§

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