1.0.0[][src]Trait ontio_std::prelude::From

pub trait From<T> {
#[lang = "from"]    pub fn from(T) -> Self;
}

Used to do value-to-value conversions while consuming the input value. It is the reciprocal of Into.

One should always prefer implementing From over Into because implementing From automatically provides one with an implementation of Into thanks to the blanket implementation in the standard library.

Only implement Into when targeting a version prior to Rust 1.41 and converting to a type outside the current crate. From was not able to do these types of conversions in earlier versions because of Rust's orphaning rules. See Into for more details.

Prefer using Into over using From when specifying trait bounds on a generic function. This way, types that directly implement Into can be used as arguments as well.

The From is also very useful when performing error handling. When constructing a function that is capable of failing, the return type will generally be of the form Result<T, E>. The From trait simplifies error handling by allowing a function to return a single error type that encapsulate multiple error types. See the "Examples" section and the book for more details.

Note: This trait must not fail. If the conversion can fail, use TryFrom.

Generic Implementations

  • From<T> for U implies Into<U> for T
  • From is reflexive, which means that From<T> for T is implemented

Examples

String implements From<&str>:

An explicit conversion from a &str to a String is done as follows:

let string = "hello".to_string();
let other_string = String::from("hello");

assert_eq!(string, other_string);

While performing error handling it is often useful to implement From for your own error type. By converting underlying error types to our own custom error type that encapsulates the underlying error type, we can return a single error type without losing information on the underlying cause. The '?' operator automatically converts the underlying error type to our custom error type by calling Into<CliError>::into which is automatically provided when implementing From. The compiler then infers which implementation of Into should be used.

use std::fs;
use std::io;
use std::num;

enum CliError {
    IoError(io::Error),
    ParseError(num::ParseIntError),
}

impl From<io::Error> for CliError {
    fn from(error: io::Error) -> Self {
        CliError::IoError(error)
    }
}

impl From<num::ParseIntError> for CliError {
    fn from(error: num::ParseIntError) -> Self {
        CliError::ParseError(error)
    }
}

fn open_and_parse_file(file_name: &str) -> Result<i32, CliError> {
    let mut contents = fs::read_to_string(&file_name)?;
    let num: i32 = contents.trim().parse()?;
    Ok(num)
}

Required methods

#[lang = "from"]pub fn from(T) -> Self[src]

Performs the conversion.

Loading content...

Implementations on Foreign Types

impl From<NonZeroU8> for NonZeroU16[src]

Converts NonZeroU8 to NonZeroU16 losslessly.

impl<'_, T> From<&'_ T> for NonNull<T> where
    T: ?Sized
[src]

impl<T> From<T> for Poll<T>[src]

pub fn from(t: T) -> Poll<T>[src]

Convert to a Ready variant.

Example

assert_eq!(Poll::from(true), Poll::Ready(true));

impl From<Infallible> for TryFromSliceError[src]

impl From<NonZeroI16> for NonZeroI64[src]

Converts NonZeroI16 to NonZeroI64 losslessly.

impl From<NonZeroU8> for NonZeroI16[src]

Converts NonZeroU8 to NonZeroI16 losslessly.

impl From<NonZeroU8> for NonZeroIsize[src]

Converts NonZeroU8 to NonZeroIsize losslessly.

impl From<!> for TryFromIntError[src]

impl From<Infallible> for TryFromIntError[src]

impl From<NonZeroU8> for NonZeroI64[src]

Converts NonZeroU8 to NonZeroI64 losslessly.

impl From<NonZeroU8> for NonZeroI32[src]

Converts NonZeroU8 to NonZeroI32 losslessly.

impl From<NonZeroI8> for NonZeroI32[src]

Converts NonZeroI8 to NonZeroI32 losslessly.

impl From<bool> for AtomicBool[src]

pub fn from(b: bool) -> AtomicBool[src]

Converts a bool into an AtomicBool.

Examples

use std::sync::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{:?}", atomic_bool), "true")

impl From<u128> for AtomicU128[src]

pub fn from(v: u128) -> AtomicU128[src]

Converts an u128 into an AtomicU128.

impl From<NonZeroU32> for NonZeroI64[src]

Converts NonZeroU32 to NonZeroI64 losslessly.

impl From<NonZeroU8> for NonZeroU32[src]

Converts NonZeroU8 to NonZeroU32 losslessly.

impl<T> From<*mut T> for AtomicPtr<T>[src]

impl From<NonZeroU16> for NonZeroI32[src]

Converts NonZeroU16 to NonZeroI32 losslessly.

impl From<NonZeroI16> for NonZeroIsize[src]

Converts NonZeroI16 to NonZeroIsize losslessly.

impl<T> From<Unique<T>> for NonNull<T> where
    T: ?Sized
[src]

impl From<NonZeroU32> for NonZeroI128[src]

Converts NonZeroU32 to NonZeroI128 losslessly.

impl From<NonZeroI8> for NonZeroI16[src]

Converts NonZeroI8 to NonZeroI16 losslessly.

impl From<NonZeroI32> for NonZeroI128[src]

Converts NonZeroI32 to NonZeroI128 losslessly.

impl From<isize> for AtomicIsize[src]

pub fn from(v: isize) -> AtomicIsize[src]

Converts an isize into an AtomicIsize.

impl From<NonZeroU16> for NonZeroI128[src]

Converts NonZeroU16 to NonZeroI128 losslessly.

impl<T> From<T> for RefCell<T>[src]

impl From<NonZeroU64> for NonZeroI128[src]

Converts NonZeroU64 to NonZeroI128 losslessly.

impl<T> From<T> for Cell<T>[src]

impl From<NonZeroU16> for NonZeroI64[src]

Converts NonZeroU16 to NonZeroI64 losslessly.

impl From<NonZeroU8> for NonZeroUsize[src]

Converts NonZeroU8 to NonZeroUsize losslessly.

impl From<!> for Infallible[src]

impl From<NonZeroI32> for NonZeroI64[src]

Converts NonZeroI32 to NonZeroI64 losslessly.

impl From<NonZeroI8> for NonZeroI128[src]

Converts NonZeroI8 to NonZeroI128 losslessly.

impl From<NonZeroU8> for NonZeroI128[src]

Converts NonZeroU8 to NonZeroI128 losslessly.

impl From<u8> for AtomicU8[src]

pub fn from(v: u8) -> AtomicU8[src]

Converts an u8 into an AtomicU8.

impl From<i16> for AtomicI16[src]

pub fn from(v: i16) -> AtomicI16[src]

Converts an i16 into an AtomicI16.

impl From<NonZeroI64> for NonZeroI128[src]

Converts NonZeroI64 to NonZeroI128 losslessly.

impl From<i32> for AtomicI32[src]

pub fn from(v: i32) -> AtomicI32[src]

Converts an i32 into an AtomicI32.

impl From<NonZeroU32> for NonZeroU64[src]

Converts NonZeroU32 to NonZeroU64 losslessly.

impl From<NonZeroU16> for NonZeroU128[src]

Converts NonZeroU16 to NonZeroU128 losslessly.

impl From<NonZeroI16> for NonZeroI32[src]

Converts NonZeroI16 to NonZeroI32 losslessly.

impl<T> From<T> for OnceCell<T>[src]

impl From<u32> for AtomicU32[src]

pub fn from(v: u32) -> AtomicU32[src]

Converts an u32 into an AtomicU32.

impl From<NonZeroI8> for NonZeroIsize[src]

Converts NonZeroI8 to NonZeroIsize losslessly.

impl From<NonZeroU16> for NonZeroUsize[src]

Converts NonZeroU16 to NonZeroUsize losslessly.

impl From<NonZeroU16> for NonZeroU32[src]

Converts NonZeroU16 to NonZeroU32 losslessly.

impl From<u64> for AtomicU64[src]

pub fn from(v: u64) -> AtomicU64[src]

Converts an u64 into an AtomicU64.

impl From<NonZeroU64> for NonZeroU128[src]

Converts NonZeroU64 to NonZeroU128 losslessly.

impl From<usize> for AtomicUsize[src]

pub fn from(v: usize) -> AtomicUsize[src]

Converts an usize into an AtomicUsize.

impl From<i64> for AtomicI64[src]

pub fn from(v: i64) -> AtomicI64[src]

Converts an i64 into an AtomicI64.

impl<T> From<T> for UnsafeCell<T>[src]

impl From<NonZeroI16> for NonZeroI128[src]

Converts NonZeroI16 to NonZeroI128 losslessly.

impl From<u16> for AtomicU16[src]

pub fn from(v: u16) -> AtomicU16[src]

Converts an u16 into an AtomicU16.

impl<'_, T> From<&'_ mut T> for NonNull<T> where
    T: ?Sized
[src]

impl From<NonZeroU8> for NonZeroU128[src]

Converts NonZeroU8 to NonZeroU128 losslessly.

impl From<i128> for AtomicI128[src]

pub fn from(v: i128) -> AtomicI128[src]

Converts an i128 into an AtomicI128.

impl From<NonZeroU8> for NonZeroU64[src]

Converts NonZeroU8 to NonZeroU64 losslessly.

impl From<i8> for AtomicI8[src]

pub fn from(v: i8) -> AtomicI8[src]

Converts an i8 into an AtomicI8.

impl From<NonZeroU32> for NonZeroU128[src]

Converts NonZeroU32 to NonZeroU128 losslessly.

impl From<NonZeroI8> for NonZeroI64[src]

Converts NonZeroI8 to NonZeroI64 losslessly.

impl From<NonZeroU16> for NonZeroU64[src]

Converts NonZeroU16 to NonZeroU64 losslessly.

impl<'a, T> From<&'a [T]> for Cow<'a, [T]> where
    T: Clone
[src]

impl<'a, T> From<Vec<T, Global>> for Cow<'a, [T]> where
    T: Clone
[src]

impl<T> From<Vec<T, Global>> for VecDeque<T>[src]

pub fn from(other: Vec<T, Global>) -> VecDeque<T>[src]

Turn a Vec<T> into a VecDeque<T>.

This avoids reallocating where possible, but the conditions for that are strict, and subject to change, and so shouldn't be relied upon unless the Vec<T> came from From<VecDeque<T>> and hasn't been reallocated.

impl<'a> From<String> for Cow<'a, str>[src]

impl<'a, B> From<Cow<'a, B>> for Arc<B> where
    B: ToOwned + ?Sized,
    Arc<B>: From<&'a B>,
    Arc<B>: From<<B as ToOwned>::Owned>, 
[src]

impl<'_, T> From<&'_ [T]> for Rc<[T]> where
    T: Clone
[src]

impl<T> From<T> for Rc<T>[src]

impl<'a, T> From<&'a Vec<T, Global>> for Cow<'a, [T]> where
    T: Clone
[src]

impl<T> From<Vec<T, Global>> for Arc<[T]>[src]

impl From<String> for Arc<str>[src]

impl<'_> From<&'_ str> for Rc<str>[src]

impl<T> From<Box<T, Global>> for Arc<T> where
    T: ?Sized
[src]

impl<T> From<T> for Arc<T>[src]

impl<T> From<Vec<T, Global>> for BinaryHeap<T> where
    T: Ord
[src]

pub fn from(vec: Vec<T, Global>) -> BinaryHeap<T>[src]

Converts a Vec<T> into a BinaryHeap<T>.

This conversion happens in-place, and has O(n) time complexity.

impl<W> From<Arc<W>> for Waker where
    W: 'static + Wake + Send + Sync
[src]

impl<'a, B> From<Cow<'a, B>> for Rc<B> where
    B: ToOwned + ?Sized,
    Rc<B>: From<&'a B>,
    Rc<B>: From<<B as ToOwned>::Owned>, 
[src]

impl<W> From<Arc<W>> for RawWaker where
    W: 'static + Wake + Send + Sync
[src]

impl<T, A> From<Box<T, A>> for Pin<Box<T, A>> where
    A: Allocator + 'static,
    T: ?Sized
[src]

pub fn from(boxed: Box<T, A>) -> Pin<Box<T, A>>[src]

Converts a Box<T> into a Pin<Box<T>>

This conversion does not allocate on the heap and happens in place.

impl<T> From<Box<T, Global>> for Rc<T> where
    T: ?Sized
[src]

impl From<LayoutError> for TryReserveError[src]

impl<'a> From<&'a str> for Cow<'a, str>[src]

impl<T> From<Vec<T, Global>> for Rc<[T]>[src]

impl<'_, T> From<&'_ [T]> for Arc<[T]> where
    T: Clone
[src]

impl From<String> for Rc<str>[src]

impl<'_> From<&'_ str> for Arc<str>[src]

impl<'a> From<&'a String> for Cow<'a, str>[src]

impl From<Words> for Bytes

impl From<Pages> for Bytes

impl From<Pages> for Bytes

impl From<Words> for Bytes

Loading content...

Implementors

impl From<String> for Box<str, Global>[src]

pub fn from(s: String) -> Box<str, Global>

Notable traits for Box<F, A>

impl<F, A> Future for Box<F, A> where
    A: Allocator + 'static,
    F: Unpin + Future + ?Sized
type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
    A: Allocator,
    I: Iterator + ?Sized
type Item = <I as Iterator>::Item;
[src]

Converts the given String to a boxed str slice that is owned.

Examples

Basic usage:

let s1: String = String::from("hello world");
let s2: Box<str> = Box::from(s1);
let s3: String = String::from(s2);

assert_eq!("hello world", s3)

impl From<String> for Vec<u8, Global>[src]

pub fn from(string: String) -> Vec<u8, Global>[src]

Converts the given String to a vector Vec that holds values of type u8.

Examples

Basic usage:

let s1 = String::from("hello world");
let v1 = Vec::from(s1);

for b in v1 {
    println!("{}", b);
}

impl From<U128> for U256[src]

impl From<Box<str, Global>> for String[src]

pub fn from(s: Box<str, Global>) -> String[src]

Converts the given boxed str slice to a String. It is notable that the str slice is owned.

Examples

Basic usage:

let s1: String = String::from("hello world");
let s2: Box<str> = s1.into_boxed_str();
let s3: String = String::from(s2);

assert_eq!("hello world", s3)

impl From<H160> for [u8; 20][src]

impl From<H256> for [u8; 32][src]

impl From<NonZeroI8> for i8[src]

pub fn from(nonzero: NonZeroI8) -> i8[src]

Converts a NonZeroI8 into an i8

impl From<NonZeroI16> for i16[src]

pub fn from(nonzero: NonZeroI16) -> i16[src]

Converts a NonZeroI16 into an i16

impl From<NonZeroI32> for i32[src]

pub fn from(nonzero: NonZeroI32) -> i32[src]

Converts a NonZeroI32 into an i32

impl From<NonZeroI64> for i64[src]

pub fn from(nonzero: NonZeroI64) -> i64[src]

Converts a NonZeroI64 into an i64

impl From<NonZeroI128> for i128[src]

pub fn from(nonzero: NonZeroI128) -> i128[src]

Converts a NonZeroI128 into an i128

impl From<NonZeroIsize> for isize[src]

pub fn from(nonzero: NonZeroIsize) -> isize[src]

Converts a NonZeroIsize into an isize

impl From<NonZeroU8> for u8[src]

pub fn from(nonzero: NonZeroU8) -> u8[src]

Converts a NonZeroU8 into an u8

impl From<NonZeroU16> for u16[src]

pub fn from(nonzero: NonZeroU16) -> u16[src]

Converts a NonZeroU16 into an u16

impl From<NonZeroU32> for u32[src]

pub fn from(nonzero: NonZeroU32) -> u32[src]

Converts a NonZeroU32 into an u32

impl From<NonZeroU64> for u64[src]

pub fn from(nonzero: NonZeroU64) -> u64[src]

Converts a NonZeroU64 into an u64

impl From<NonZeroU128> for u128[src]

pub fn from(nonzero: NonZeroU128) -> u128[src]

Converts a NonZeroU128 into an u128

impl From<NonZeroUsize> for usize[src]

pub fn from(nonzero: NonZeroUsize) -> usize[src]

Converts a NonZeroUsize into an usize

impl From<[u8; 20]> for H160[src]

pub fn from(bytes: [u8; 20]) -> Self[src]

Constructs a hash type from the given bytes array of fixed length.

Note

The given bytes are interpreted in big endian order.

impl From<[u8; 32]> for H256[src]

pub fn from(bytes: [u8; 32]) -> Self[src]

Constructs a hash type from the given bytes array of fixed length.

Note

The given bytes are interpreted in big endian order.

impl From<bool> for i8[src]

Converts a bool to a i8. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(i8::from(true), 1);
assert_eq!(i8::from(false), 0);

impl From<bool> for i16[src]

Converts a bool to a i16. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(i16::from(true), 1);
assert_eq!(i16::from(false), 0);

impl From<bool> for i32[src]

Converts a bool to a i32. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(i32::from(true), 1);
assert_eq!(i32::from(false), 0);

impl From<bool> for i64[src]

Converts a bool to a i64. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(i64::from(true), 1);
assert_eq!(i64::from(false), 0);

impl From<bool> for i128[src]

Converts a bool to a i128. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(i128::from(true), 1);
assert_eq!(i128::from(false), 0);

impl From<bool> for isize[src]

Converts a bool to a isize. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(isize::from(true), 1);
assert_eq!(isize::from(false), 0);

impl From<bool> for u8[src]

Converts a bool to a u8. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u8::from(true), 1);
assert_eq!(u8::from(false), 0);

impl From<bool> for u16[src]

Converts a bool to a u16. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u16::from(true), 1);
assert_eq!(u16::from(false), 0);

impl From<bool> for u32[src]

Converts a bool to a u32. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u32::from(true), 1);
assert_eq!(u32::from(false), 0);

impl From<bool> for u64[src]

Converts a bool to a u64. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u64::from(true), 1);
assert_eq!(u64::from(false), 0);

impl From<bool> for u128[src]

Converts a bool to a u128. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u128::from(true), 1);
assert_eq!(u128::from(false), 0);

impl From<bool> for usize[src]

Converts a bool to a usize. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(usize::from(true), 1);
assert_eq!(usize::from(false), 0);

impl From<char> for String[src]

impl From<char> for u32[src]

pub fn from(c: char) -> u32[src]

Converts a char into a u32.

Examples

use std::mem;

let c = 'c';
let u = u32::from(c);
assert!(4 == mem::size_of_val(&u))

impl From<f32> for f64[src]

Converts f32 to f64 losslessly.

impl From<i8> for f32[src]

Converts i8 to f32 losslessly.

impl From<i8> for f64[src]

Converts i8 to f64 losslessly.

impl From<i8> for i16[src]

Converts i8 to i16 losslessly.

impl From<i8> for i32[src]

Converts i8 to i32 losslessly.

impl From<i8> for i64[src]

Converts i8 to i64 losslessly.

impl From<i8> for i128[src]

Converts i8 to i128 losslessly.

impl From<i8> for isize[src]

Converts i8 to isize losslessly.

impl From<i16> for f32[src]

Converts i16 to f32 losslessly.

impl From<i16> for f64[src]

Converts i16 to f64 losslessly.

impl From<i16> for i32[src]

Converts i16 to i32 losslessly.

impl From<i16> for i64[src]

Converts i16 to i64 losslessly.

impl From<i16> for i128[src]

Converts i16 to i128 losslessly.

impl From<i16> for isize[src]

Converts i16 to isize losslessly.

impl From<i32> for f64[src]

Converts i32 to f64 losslessly.

impl From<i32> for i64[src]

Converts i32 to i64 losslessly.

impl From<i32> for i128[src]

Converts i32 to i128 losslessly.

impl From<i64> for i128[src]

Converts i64 to i128 losslessly.

impl From<u8> for char[src]

Maps a byte in 0x00..=0xFF to a char whose code point has the same value, in U+0000..=U+00FF.

Unicode is designed such that this effectively decodes bytes with the character encoding that IANA calls ISO-8859-1. This encoding is compatible with ASCII.

Note that this is different from ISO/IEC 8859-1 a.k.a. ISO 8859-1 (with one less hyphen), which leaves some "blanks", byte values that are not assigned to any character. ISO-8859-1 (the IANA one) assigns them to the C0 and C1 control codes.

Note that this is also different from Windows-1252 a.k.a. code page 1252, which is a superset ISO/IEC 8859-1 that assigns some (not all!) blanks to punctuation and various Latin characters.

To confuse things further, on the Web ascii, iso-8859-1, and windows-1252 are all aliases for a superset of Windows-1252 that fills the remaining blanks with corresponding C0 and C1 control codes.

pub fn from(i: u8) -> char[src]

Converts a u8 into a char.

Examples

use std::mem;

let u = 32 as u8;
let c = char::from(u);
assert!(4 == mem::size_of_val(&c))

impl From<u8> for f32[src]

Converts u8 to f32 losslessly.

impl From<u8> for f64[src]

Converts u8 to f64 losslessly.

impl From<u8> for i16[src]

Converts u8 to i16 losslessly.

impl From<u8> for i32[src]

Converts u8 to i32 losslessly.

impl From<u8> for i64[src]

Converts u8 to i64 losslessly.

impl From<u8> for i128[src]

Converts u8 to i128 losslessly.

impl From<u8> for isize[src]

Converts u8 to isize losslessly.

impl From<u8> for u16[src]

Converts u8 to u16 losslessly.

impl From<u8> for u32[src]

Converts u8 to u32 losslessly.

impl From<u8> for u64[src]

Converts u8 to u64 losslessly.

impl From<u8> for u128[src]

Converts u8 to u128 losslessly.

impl From<u8> for usize[src]

Converts u8 to usize losslessly.

impl From<u16> for f32[src]

Converts u16 to f32 losslessly.

impl From<u16> for f64[src]

Converts u16 to f64 losslessly.

impl From<u16> for i32[src]

Converts u16 to i32 losslessly.

impl From<u16> for i64[src]

Converts u16 to i64 losslessly.

impl From<u16> for i128[src]

Converts u16 to i128 losslessly.

impl From<u16> for u32[src]

Converts u16 to u32 losslessly.

impl From<u16> for u64[src]

Converts u16 to u64 losslessly.

impl From<u16> for u128[src]

Converts u16 to u128 losslessly.

impl From<u16> for usize[src]

Converts u16 to usize losslessly.

impl From<u32> for f64[src]

Converts u32 to f64 losslessly.

impl From<u32> for i64[src]

Converts u32 to i64 losslessly.

impl From<u32> for i128[src]

Converts u32 to i128 losslessly.

impl From<u32> for u64[src]

Converts u32 to u64 losslessly.

impl From<u32> for u128[src]

Converts u32 to u128 losslessly.

impl From<u64> for i128[src]

Converts u64 to i128 losslessly.

impl From<u64> for u128[src]

Converts u64 to u128 losslessly.

impl From<u128> for U256[src]

impl<'_> From<&'_ String> for String[src]

impl<'_> From<&'_ mut str> for String[src]

pub fn from(s: &mut str) -> String[src]

Converts a &mut str into a String.

The result is allocated on the heap.

impl<'_> From<&'_ str> for String[src]

impl<'_> From<&'_ str> for Box<str, Global>[src]

pub fn from(s: &str) -> Box<str, Global>

Notable traits for Box<F, A>

impl<F, A> Future for Box<F, A> where
    A: Allocator + 'static,
    F: Unpin + Future + ?Sized
type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
    A: Allocator,
    I: Iterator + ?Sized
type Item = <I as Iterator>::Item;
[src]

Converts a &str into a Box<str>

This conversion allocates on the heap and performs a copy of s.

Examples

let boxed: Box<str> = Box::from("hello");
println!("{}", boxed);

impl<'_> From<&'_ str> for Vec<u8, Global>[src]

impl<'_> From<Cow<'_, str>> for Box<str, Global>[src]

impl<'_, T> From<&'_ [T]> for Box<[T], Global> where
    T: Copy
[src]

pub fn from(slice: &[T]) -> Box<[T], Global>

Notable traits for Box<F, A>

impl<F, A> Future for Box<F, A> where
    A: Allocator + 'static,
    F: Unpin + Future + ?Sized
type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
    A: Allocator,
    I: Iterator + ?Sized
type Item = <I as Iterator>::Item;
[src]

Converts a &[T] into a Box<[T]>

This conversion allocates on the heap and performs a copy of slice.

Examples

// create a &[u8] which will be used to create a Box<[u8]>
let slice: &[u8] = &[104, 101, 108, 108, 111];
let boxed_slice: Box<[u8]> = Box::from(slice);

println!("{:?}", boxed_slice);

impl<'_, T> From<&'_ [T]> for Vec<T, Global> where
    T: Clone
[src]

impl<'_, T> From<&'_ mut [T]> for Vec<T, Global> where
    T: Clone
[src]

impl<'_, T> From<Cow<'_, [T]>> for Box<[T], Global> where
    T: Copy
[src]

impl<'a> From<&'a [u8; 20]> for H160[src]

pub fn from(bytes: &'a [u8; 20]) -> Self[src]

Constructs a hash type from the given reference to the bytes array of fixed length.

Note

The given bytes are interpreted in big endian order.

impl<'a> From<&'a [u8; 32]> for H256[src]

pub fn from(bytes: &'a [u8; 32]) -> Self[src]

Constructs a hash type from the given reference to the bytes array of fixed length.

Note

The given bytes are interpreted in big endian order.

impl<'a> From<&'a mut [u8; 20]> for H160[src]

pub fn from(bytes: &'a mut [u8; 20]) -> Self[src]

Constructs a hash type from the given reference to the mutable bytes array of fixed length.

Note

The given bytes are interpreted in big endian order.

impl<'a> From<&'a mut [u8; 32]> for H256[src]

pub fn from(bytes: &'a mut [u8; 32]) -> Self[src]

Constructs a hash type from the given reference to the mutable bytes array of fixed length.

Note

The given bytes are interpreted in big endian order.

impl<'a> From<Cow<'a, str>> for String[src]

impl<'a, T> From<&'a Option<T>> for Option<&'a T>[src]

pub fn from(o: &'a Option<T>) -> Option<&'a T>[src]

Converts from &Option<T> to Option<&T>.

Examples

Converts an Option<String> into an Option<usize>, preserving the original. The map method takes the self argument by value, consuming the original, so this technique uses as_ref to first take an Option to a reference to the value inside the original.

let s: Option<String> = Some(String::from("Hello, Rustaceans!"));
let o: Option<usize> = Option::from(&s).map(|ss: &String| ss.len());

println!("Can still print s: {:?}", s);

assert_eq!(o, Some(18));

impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T>[src]

pub fn from(o: &'a mut Option<T>) -> Option<&'a mut T>[src]

Converts from &mut Option<T> to Option<&mut T>

Examples

let mut s = Some(String::from("Hello"));
let o: Option<&mut String> = Option::from(&mut s);

match o {
    Some(t) => *t = String::from("Hello, Rustaceans!"),
    None => (),
}

assert_eq!(s, Some(String::from("Hello, Rustaceans!")));

impl<'a, T> From<Cow<'a, [T]>> for Vec<T, Global> where
    [T]: ToOwned,
    <[T] as ToOwned>::Owned == Vec<T, Global>, 
[src]

impl<A> From<Box<str, A>> for Box<[u8], A> where
    A: Allocator
[src]

pub fn from(s: Box<str, A>) -> Box<[u8], A>

Notable traits for Box<F, A>

impl<F, A> Future for Box<F, A> where
    A: Allocator + 'static,
    F: Unpin + Future + ?Sized
type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
    A: Allocator,
    I: Iterator + ?Sized
type Item = <I as Iterator>::Item;
[src]

Converts a Box<str> into a Box<[u8]>

This conversion does not allocate on the heap and happens in place.

Examples

// create a Box<str> which will be used to create a Box<[u8]>
let boxed: Box<str> = Box::from("hello");
let boxed_str: Box<[u8]> = Box::from(boxed);

// create a &[u8] which will be used to create a Box<[u8]>
let slice: &[u8] = &[104, 101, 108, 108, 111];
let boxed_slice = Box::from(slice);

assert_eq!(boxed_slice, boxed_str);

impl<T> From<!> for T[src]

Stability note: This impl does not yet exist, but we are "reserving space" to add it in the future. See rust-lang/rust#64715 for details.

impl<T> From<BinaryHeap<T>> for Vec<T, Global>[src]

pub fn from(heap: BinaryHeap<T>) -> Vec<T, Global>[src]

Converts a BinaryHeap<T> into a Vec<T>.

This conversion requires no data movement or allocation, and has constant time complexity.

impl<T> From<VecDeque<T>> for Vec<T, Global>[src]

pub fn from(other: VecDeque<T>) -> Vec<T, Global>[src]

Turn a VecDeque<T> into a Vec<T>.

This never needs to re-allocate, but does need to do O(n) data movement if the circular buffer doesn't happen to be at the beginning of the allocation.

Examples

use std::collections::VecDeque;

// This one is *O*(1).
let deque: VecDeque<_> = (1..5).collect();
let ptr = deque.as_slices().0.as_ptr();
let vec = Vec::from(deque);
assert_eq!(vec, [1, 2, 3, 4]);
assert_eq!(vec.as_ptr(), ptr);

// This one needs data rearranging.
let mut deque: VecDeque<_> = (1..5).collect();
deque.push_front(9);
deque.push_front(8);
let ptr = deque.as_slices().1.as_ptr();
let vec = Vec::from(deque);
assert_eq!(vec, [8, 9, 1, 2, 3, 4]);
assert_eq!(vec.as_ptr(), ptr);

impl<T> From<T> for Option<T>[src]

pub fn from(val: T) -> Option<T>[src]

Copies val into a new Some.

Examples

let o: Option<u8> = Option::from(67);

assert_eq!(Some(67), o);

impl<T> From<T> for Box<T, Global>[src]

pub fn from(t: T) -> Box<T, Global>

Notable traits for Box<F, A>

impl<F, A> Future for Box<F, A> where
    A: Allocator + 'static,
    F: Unpin + Future + ?Sized
type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
    A: Allocator,
    I: Iterator + ?Sized
type Item = <I as Iterator>::Item;
[src]

Converts a generic type T into a Box<T>

The conversion allocates on the heap and moves t from the stack into it.

Examples

let x = 5;
let boxed = Box::new(5);

assert_eq!(Box::from(x), boxed);

impl<T> From<T> for T[src]

impl<T, A> From<Box<[T], A>> for Vec<T, A> where
    A: Allocator
[src]

impl<T, A> From<Vec<T, A>> for Box<[T], A> where
    A: Allocator
[src]

impl<T, const N: usize> From<[T; N]> for Box<[T], Global>[src]

pub fn from(array: [T; N]) -> Box<[T], Global>

Notable traits for Box<F, A>

impl<F, A> Future for Box<F, A> where
    A: Allocator + 'static,
    F: Unpin + Future + ?Sized
type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
    A: Allocator,
    I: Iterator + ?Sized
type Item = <I as Iterator>::Item;
[src]

Converts a [T; N] into a Box<[T]>

This conversion moves the array to newly heap-allocated memory.

Examples

let boxed: Box<[u8]> = Box::from([4, 2]);
println!("{:?}", boxed);

impl<T, const N: usize> From<[T; N]> for Vec<T, Global>[src]

Loading content...