You could use From::from
:
use std::convert::*;
struct NewType(pub i32);
impl From<NewType> for i32 {
fn from(src: NewType) -> i32 {
src.0
}
}
fn main() {
let a = NewType(5);
println!("{}", i32::from(a));
}
You can read more about it in the docs for the convert
module.
More Related Contents:
- What are non-lexical lifetimes?
- What are Rust’s exact auto-dereferencing rules?
- Why is a trait not implemented for a type that clearly has it implemented?
- How do I implement a trait I don’t own for a type I don’t own?
- Mutably borrow one struct field while borrowing another in a closure
- What makes something a “trait object”?
- How do I convert a Vector of bytes (u8) to a string?
- Is it possible to use `impl Trait` as a function’s return type in a trait definition?
- Cannot borrow as immutable because it is also borrowed as mutable in function arguments
- What is the relation between auto-dereferencing and deref coercion?
- What is the return type of the indexing operation?
- How to do a binary search on a Vec of floats?
- Change enum variant while moving the field to the new variant
- I implemented a trait for another trait but cannot call methods from both traits
- Lifetime troubles sharing references between threads
- Moved variable still borrowing after calling `drop`?
- How do I include the end value in a range?
- How do I invoke a system command and capture its output?
- Can I destructure a tuple without binding the result to a new variable in a let/match/for statement?
- How to format output to a byte array with no_std and no allocator?
- Temporary value is freed at the end of this statement [duplicate]
- How can I store a pattern in a variable in Rust?
- Why does a mutable borrow of a closure through DerefMut not work?
- How to switch between Rust toolchains?
- Why can Serde not derive Deserialize for a struct containing only a &Path?
- How do I specify that a struct field must implement a trait?
- How to create and write to memory mapped files?
- Downcast traits inside Rc for AST manipulation
- How to check in Rust if architecture is 32 or 64 bit?
- Is there any way to restrict a generic type to one of several types?