-
-
Save sancho20021/d6faa25ef319db2442cdfbb0aef6b9a0 to your computer and use it in GitHub Desktop.
Code used in "Structs in Rust" article
This file contains 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
#[allow(dead_code, unused_variables)] | |
fn main() { | |
// ----------------Defining struct---------------- | |
// Ordinary struct | |
#[derive(Debug, Eq, PartialEq, Default)] | |
struct Point { | |
x: i32, | |
y: i32, | |
} | |
// Tuple struct | |
struct PointTuple(i32, i32); | |
// ----------------Initializing struct---------------- | |
let p_named = Point { x: 13, y: 37 }; | |
let p_unnamed = PointTuple(13, 37); | |
// ----------------Field access---------------- | |
let x = p_named.x; | |
let y = p_named.y; | |
let x = p_unnamed.0; | |
let y = p_unnamed.1; | |
let mut p = PointTuple(1, 2); | |
p.0 = 1000; | |
assert_eq!(p.0, 1000); | |
// ----------------Field init syntax---------------- | |
struct Bicycle { | |
brand: String, | |
kind: String, | |
size: u16, | |
suspension: bool, | |
} | |
let b1 = Bicycle { | |
brand: String::from("Brand A"), | |
kind: String::from("Mtb"), | |
size: 56, | |
suspension: true, | |
}; | |
let b2 = Bicycle { | |
brand: String::from("Other brand"), | |
..b1 | |
}; | |
fn new_bicycle(brand: String, kind: String) -> Bicycle { | |
Bicycle { | |
brand, | |
// ^^^ instead of "brand: brand" | |
kind, | |
// ^^^ instead of "kind: kind" | |
size: 54, | |
suspension: false, | |
} | |
} | |
// ----------------Trait usage---------------- | |
let p = Point { x: 0, y: 1 }; | |
println!("{:?}", p); | |
let a = Point { x: 25, y: 50 }; | |
let b = Point { x: 100, y: 100 }; | |
let c = Point { x: 25, y: 50 }; | |
assert!(a == c); | |
assert!(a != b); | |
assert!(b != c); | |
#[derive(Debug, PartialEq, Eq, Default)] | |
struct DoubleBool(bool, bool); | |
let point: Point = Default::default(); | |
let double_bool: DoubleBool = Default::default(); | |
assert_eq!(point, Point { x: 0, y: 0 }); | |
assert_eq!(double_bool, DoubleBool(false, false)); | |
// ----------------Methods---------------- | |
impl Point { | |
// Method that can't modify the struct instance | |
fn has_same_x(&self, other: &Self) -> bool { | |
self.x == other.x | |
} | |
// Method that can modify the struct instance | |
fn shift_right(&mut self, dx: i32) { | |
self.x += dx; | |
} | |
} | |
// Mutable binding | |
let mut point = Point { x: 25, y: 25 }; | |
assert!(!point.has_same_x(&Point { x: 30, y: 25 })); | |
// Calling a method that changes the object state | |
point.shift_right(3); | |
assert_eq!(point.x, 28); | |
impl Point { | |
// Associated function | |
fn new(x: i32, y: i32) -> Point { | |
Point { x, y } | |
} | |
} | |
// Using associated functions | |
let point = Point::new(1, 2); | |
// Autoreferencing | |
point.shift_right(3); | |
(&mut point).shift_right(3); | |
// shift_right(&mut point, 3); | |
// Manual trait implementation | |
impl std::fmt::Display for Point { | |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
write!(f, "({}, {})", self.x, self.y) | |
} | |
} | |
let p = Point::new(0, 1); | |
// format a pair | |
assert_eq!(format!("{:?}", p), "Point { x: 0, y: 1 }"); | |
assert_eq!(format!("{}", p), "(0, 1)"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment