Advertisement
cwchen

[Rust] C-style object

Aug 23rd, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.66 KB | None | 0 0
  1. struct Point {
  2.     x: f64,
  3.     y: f64,
  4. }
  5.  
  6. fn point_new(x: f64, y: f64) -> Point {
  7.     Point{ x: x, y: y }
  8. }
  9.  
  10. fn point_get_x(p: & Point) -> f64 {
  11.     (*p).x
  12. }
  13.  
  14. fn point_get_y(p: & Point) -> f64 {
  15.     (*p).y
  16. }
  17.  
  18. fn point_set_x(p: &mut Point, x: f64) {
  19.     (*p).x = x;
  20. }
  21.  
  22. fn point_set_y(p: &mut Point, y: f64) {
  23.     (*p).y = y;
  24.  }
  25.  
  26. fn point_to_string(p: & Point) -> String {
  27.     format!("({}, {})", point_get_x(p), point_get_y(p))
  28. }
  29.  
  30. fn main() {
  31.     let mut p = point_new(0.0, 0.0);
  32.     assert_eq!(point_to_string(&p), "(0, 0)");
  33.  
  34.     point_set_x(&mut p, 3.0);
  35.     point_set_y(&mut p, 4.0);
  36.     assert_eq!(point_to_string(&p), "(3, 4)");
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement