@swissgathu
a rust primer
Rust means never having to close a socket
~ Yehuda Katz
fn main() {
for count in 0..3 {
println!("{}. Welcome to Andela!", count);
}
}
pub trait Summarizable {
fn summary(&self) -> String;
}
pub struct Tweet {
pub username: String,
pub content: String,
pub reply: bool,
pub retweet: bool,
}
impl Summarizable for Tweet {
fn summary(&self) -> String {
format!("{}: {}", self.username, self.content)
}
}
TRAITS
PYTHON
C ABI + header (rust) & CFFI (python)
RUST
def build_native(spec):
"""Build rust binary"""
# Step 1: build the rust library
build = spec.add_external_build(
cmd=['cargo', 'build', '--release'],path='./rust'
)
# Step 2: add a cffi module based on the dylib we built
spec.add_cffi_module(
module_path='native._native',
dylib=....,
header_filename=...,
rtld_flags=['NOW', 'NODELETE']
)
csvterm-0.1-py2.py3-none-macosx_10_12_x86_64.whl
def tail(file, number, select, delimiter):
"""Show the last N rows of CSV data. Default is 10 rows"""
....
// Show the last N rows of CSV data. Default is 10 rows
fn rtail(fname: &str,
select: Option<Vec<usize>>,
delimiter: u8,
count: usize,) -> Result<Vec<u8>, Box<Error>> {
...
}
#[no_mangle]
pub unsafe extern "C" fn tail(
fname: *const c_char,
select: *const c_char,
delimiter: c_char,
count: i32,
) -> *const u8 {
...
let v = rtail(&fname, select, delimiter as u8, count).unwrap();
...
}
from . import _native
tail_bytes = _native.lib.tail(str.encode(filename),
str.encode(filter_cols_nums),
str.encode(delimiter),
number)
tail_string = ffi.string(bytes).decode()
tail_rows = tail_string.split('\n')