Last active
February 3, 2026 20:27
-
-
Save dacr/1c530df766657844288e5673c8910d45 to your computer and use it in GitHub Desktop.
hello rust type conversions / published by https://github.com/dacr/code-examples-manager #78987a3a-c3e9-401e-bdf6-773c55c6f3fb/32f5d6c2c394f8dc817fca70dfa6fa2adc184d04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env rust-script | |
| // summary : hello rust type conversions | |
| // keywords : rust, types, conversions, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 78987a3a-c3e9-401e-bdf6-773c55c6f3fb | |
| // created-on : 2024-10-16T09:27:59+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : ./$file | |
| #[derive(Debug, PartialEq, Clone)] | |
| struct BigPoint { | |
| x: f64, | |
| y: f64, | |
| } | |
| impl From<(f64, f64)> for BigPoint { | |
| fn from(value: (f64, f64)) -> Self { | |
| Self { x: value.0, y: value.1 } | |
| } | |
| } | |
| impl From<BigPoint> for (f64, f64) { | |
| fn from(value: BigPoint) -> (f64, f64) { | |
| (value.x, value.y) | |
| } | |
| } | |
| fn main() { | |
| let p = BigPoint::from((1.2, 3.4)); // CONVERSION FROM TUPLE TO BigPoint | |
| assert_eq!(p, BigPoint { x: 1.2, y: 3.4 }); | |
| let p: BigPoint = (0.1, 1.1).into(); // CONVERSION FROM TUPLE TO BigPoint | |
| assert_eq!(p, BigPoint { x: 0.1, y: 1.1 }); | |
| let t: (f64, f64) = p.clone().into(); // CONVERSION FROM BigPoint to TUPLE | |
| println!("{:?}, {:?}", p, t); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment