+------------------------------------------------------------------------------+
|                                                                              |
|  Dhananjaya D R                   @/logs   @/software   @/resume   @/contact |
|                                                                              |
+------------------------------------------------------------------------------+


Discovering Rust from a Python perspective
________________________________________________________________________________

While Python and Rust have distinct design philosophies and strengths, they 
share some common ground here's a comparative exploration,


Hello world!
________________________________________________________________________________

+------------------------------------------------------------------------------+
| Python                                                                       |
+------------------------------------------------------------------------------+
|  1  def main():                                                              |
|  2    for _ in range(3):                                                     |
|  3      print("Hello from Python!")                                          |
|  4  main()                                                                   |
|                                                                              |
+------------------------------------------------------------------------------+

+------------------------------------------------------------------------------+
| Rust                                                                         |
+------------------------------------------------------------------------------+
|  1  fn main() {                                                              |
|  2    for _ in 0..3 {                                                        |
|  3      println!("Hello from Rust!");                                        |
|  4    }                                                                      |
|  5  }                                                                        |
+------------------------------------------------------------------------------+


Class
________________________________________________________________________________

+------------------------------------------------------------------------------+
| Python                                                                       |
+------------------------------------------------------------------------------+
|  1  class Point:                                                             |
|  2      def __init__(self, x, y):                                            |
|  3          self.x = x                                                       |
|  4          self.y = y                                                       |
|  5                                                                           |
|  6      def move(self, dx, dy):                                              |
|  7          self.x += dx                                                     |
|  8          self.y += dy                                                     |
|  9                                                                           |
| 10      def distance_from_origin(self):                                      |
| 11          return ((self.x**2) + (self.y**2))**(1/2)                        |
| 12                                                                           |
| 13      def __str__(self):                                                   |
| 14          return f"({self.x}, {self.y})"                                   |
| 15                                                                           |
| 16  p1 = Point(3, 4)                                                         |
| 17  p2 = Point(5, 7)                                                         |
| 18                                                                           |
| 19  print(f"Distance between p1 and origin: {p1.distance_from_origin()}")    |
| 20  p2.move(2, -1)                                                           |
| 21  print(f"p2 after moving: {p2}")                                          |
+------------------------------------------------------------------------------+

+------------------------------------------------------------------------------+
| Rust                                                                         |
+------------------------------------------------------------------------------+
|  1  struct Point {                                                           |
|  2      x: i32,                                                              |
|  3      y: i32,                                                              |
|  4  }                                                                        |
|  5                                                                           |
|  6  impl Point {                                                             |
|  7      fn new(x: i32, y: i32) -> Self {                                     |
|  8          Point { x, y }                                                   |
|  9      }                                                                    |
| 10                                                                           |
| 11      fn move(&mut self, dx: i32, dy: i32) {                               |
| 12          self.x += dx;                                                    |
| 13          self.y += dy;                                                    |
| 14      }                                                                    |
| 15                                                                           |
| 16      fn distance_from_origin(&self) -> f64 {                              |
| 17          ((self.x * self.x) + (self.y * self.y)) as f64.sqrt()            |
| 18      }                                                                    |
| 19  }                                                                        |
| 20                                                                           |
| 21  impl ToString for Point {                                                |
| 22      fn to_string(&self) -> String {                                      |
| 23          format!("({}, {})", self.x, self.y)                              |
| 24      }                                                                    |
| 25  }                                                                        |
| 26                                                                           |
| 27  let p1 = Point::new(3, 4);                                               |
| 28  let p2 = Point::new(5, 7);                                               |
| 29                                                                           |
| 30  println!("Distance between p1 and origin: {}",                           |
| 31           p1.distance_from_origin());                                     |
| 31  p2.move(2, -1);                                                          |
| 32  println!("p2 after moving: {}", p2);                                     |
+------------------------------------------------------------------------------+


Error Handling
________________________________________________________________________________

+------------------------------------------------------------------------------+
| Python                                                                       |
+------------------------------------------------------------------------------+
|  1  try:                                                                     |
|  2      with open("myfile.txt", "r") as file:                                |
|  3          data = file.read()                                               |
|  4      print(data)                                                          |
|  5  except FileNotFoundError:                                                |
|  6      print("File not found!")                                             |
+------------------------------------------------------------------------------+

+------------------------------------------------------------------------------+
| Rust                                                                         |
+------------------------------------------------------------------------------+
|  1  use std::fs::File;                                                       |
|  2  use std::io::Read;                                                       |
|  3                                                                           |
|  4  match File::open("myfile.txt") {                                         |
|  5      Ok(mut file) => {                                                    |
|  6          let mut data = String::new();                                    |
|  7          file.read_to_string(&mut data)?;                                 |
|  8          println!("{}", data);                                            |
|  9      }                                                                    |
| 10      Err(error) => {                                                      |
| 11          println!("Error reading file: {}", error);                       |
| 12      }                                                                    |
| 13  }                                                                        |
+------------------------------------------------------------------------------+


abstract vs tarit
________________________________________________________________________________

+------------------------------------------------------------------------------+
| Python                                                                       |
+------------------------------------------------------------------------------+
|  1  from abc import ABC, abstractmethod                                      |
|  2                                                                           |
|  3  class Shape(ABC):                                                        |
|  4      @abstractmethod                                                      |
|  5      def area(self):                                                      |
|  6          pass                                                             |
|  7                                                                           |
|  8  class Square(Shape):                                                     |
|  9      def __init__(self, side):                                            |
| 10          self.side = side                                                 |
| 11                                                                           |
| 12      def area(self):                                                      |
| 13          return self.side * self.side                                     |
+------------------------------------------------------------------------------+

+------------------------------------------------------------------------------+
| Rust                                                                         |
+------------------------------------------------------------------------------+
|  1  trait Shape {                                                            |
|  2      fn area(&self) -> u32;                                               |
|  3  }                                                                        |
|  4                                                                           |
|  5  struct Square {                                                          |
|  6      side: u32,                                                           |
|  7  }                                                                        |
|  8                                                                           |
|  9  impl Shape for Square {                                                  |
| 10      fn area(&self) -> u32 {                                              |
| 11          self.side * self.side                                            |
| 12      }                                                                    |
| 13  }                                                                        |
+------------------------------------------------------------------------------+


Conclusion
________________________________________________________________________________

That being said, I don't think there is a direct relationship between Python and
Rust. However, I hope that more people will begin to explore Rust for areas 
where they previously used Python.



_______________________________________________________________________________