Skip to Main Content
Rust Programming Cookbook
book

Rust Programming Cookbook

by Claus Matzinger
October 2019
Intermediate to advanced content levelIntermediate to advanced
444 pages
10h 37m
English
Packt Publishing
Content preview from Rust Programming Cookbook

How to do it...

Python is a great language to integrate with—find out why in just a few steps:

  1. Open rust-digest/src/lib.rs to start off with the Rust code. Let's add the required use statements for FFI and ring and declare a digest() function to be exported. Note that this function is the same as in most other recipes in this chapter:
use std::ffi::{CStr, CString};use std::os::raw::c_char;use ring::digest;#[no_mangle]pub extern "C" fn digest(data: *mut c_char) -> *mut c_char {    unsafe {        let data = CStr::from_ptr(data);        let signature = digest::digest(&digest::SHA256,          data.to_bytes());        let hex_digest = signature            .as_ref()            .iter()            .map(|b| format!("{:X}", b))            .collect::<String>();        CString::new(hex_digest).unwrap().into_raw()    }}// No tests ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Rust Web Programming

Rust Web Programming

Maxwell Flitton
The Complete Rust Programming Reference Guide

The Complete Rust Programming Reference Guide

Rahul Sharma, Vesa Kaihlavirta, Claus Matzinger

Publisher Resources

ISBN: 9781789530667Supplemental Content