with Davis Silverman
(feel free to interrupt with questions!)
fn main() {
println!("Hello, World!");
}
#[derive(Debug)]
struct Point {
x: f64,
y: f64,
}
impl Point {
fn origin() -> Point { Point{x: 0.0, y: 0.0} }
fn new(x: f64, y: f64) -> Point { Point{x: x, y: y} }
fn distance(&self, other: &Point) -> f64 {
f64::sqrt(
f64::powi((self.x - other.x), 2) +
f64::powi((self.y - other.y), 2)
)
}
}
fn main() {
let a: Point = Point::origin();
let b = Point::new(1.0, 3.5);
let distance = b.distance(&a);
println!("The distance between {:?} and {:?} is {}", a, b, distance);
}
http://is.gd/EiIo3E
/// A Library about points and stuff.
extern crate libc;
#[repr(C)]
pub struct Point {
x: libc::c_double,
y: libc::c_double,
}
#[no_mangle]
pub extern "C" fn point_origin() -> Box<Point> {
Box::new(Point{x: 0.0, y: 0.0})
}
#[no_mangle]
pub extern "C" fn point_new(x: libc::c_double, y: libc::c_double) -> Box<Point> {
Box::new(Point{x: x, y: y}
}
#[no_mangle]
pub extern "C" fn point_distance(this: Box<Point>, other: Box<Point>) -> libc::c_double {
f64::sqrt(f64::powi((this.x - other.x), 2) +
f64::powi((this.y - other.y), 2))
}
Structure
from ctypes import Structure, c_double
class POINT(Structure):
_fields_ = [("x", c_double), ("y", c_double)]
from ctypes import Structure, CDLL, c_double, pointer, POINTER
class POINT(Structure):
_fields_ = [("x", c_double), ("y", c_double)]
rlib = CDLL("points.dll")
rlib.point_new.restype = POINTER(POINT)
rlib.point_new.argtypes = [c_double, c_double]
rlib.point_origin.restype = POINTER(POINT)
rlib.point_origin.argtypes = []
from ctypes import Structure, CDLL, c_double, pointer, POINTER
class POINT(Structure):
_fields_ = [("x", c_double), ("y", c_double)]
rlib = CDLL("points.dll")
rlib.point_new.restype = POINTER(POINT)
rlib.point_new.argtypes = [c_double, c_double]
rlib.point_origin.restype = POINTER(POINT)
rlib.point_origin.argtypes = []
a = rlib.point_new(c_double(1.0), c_double(3.5))
b = rlib.point_origin()
print("Point {{x: {}, y: {}}}".format(a.contents.x, a.contents.y))