TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Allowing Matlab to Talk to Rust

64 pointsby smitecover 9 years ago

4 comments

jblazevicover 9 years ago
Great stuff!<p>One minor suggestion, completely beside the point so please forgive me: a safer and perhaps more idiomatic way to implement the multiply_safe function would be with a functional-style one-liner:<p><pre><code> fn multiply_safe(a : Vec&lt;f64&gt;, b : Vec&lt;f64&gt;) -&gt; Vec&lt;f64&gt; { if a.len() != b.len() { panic!(&quot;The two vectors differ in length!&quot;); } return a.iter().zip(b.iter()).map(|(x, y)| x * y).collect() } </code></pre> Safer because you don&#x27;t manipulate indices and lengths directly like you would in C, so there is no opportunity of an off-by-one bug or things like that. Surprisingly, rustc optimizer makes sure that this performs as well as the index-wrangling implementation (according to some simple tests I just ran).
评论 #11048105 未加载
评论 #11043131 未加载
ampronover 9 years ago
Well explained! Here are some more examples using the calllib function to access the rust code, although I think the mex interface is probably the better way to go. <a href="https:&#x2F;&#x2F;github.com&#x2F;ampron&#x2F;rustlab" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ampron&#x2F;rustlab</a>
评论 #11033436 未加载
marmadukeover 9 years ago
Interesting, though the C middleman isn&#x27;t necessary if you can describe to Rust&#x27;s FFI that mxArray is an opaque struct (I imagine the other regular functions in the MEX API are cakewalk for an FFI worth it&#x27;s weight).<p>A while ago I wrote a mexFunction purely in Cython, and it was wondeful because Python was immediately available to work with (instead of C). The problem was that the numerical libraries on which NumPy builds often shared symbol names with MATLAB (such as svd, or hdf5) that differ in implementation resulting segfaults or corrupted data.
评论 #11043078 未加载
bluejekyllover 9 years ago
One thing that jumped out to me, and I haven&#x27;t worked with Rust FFI enough to know this answer. Is the C library necessary? Since Rust is exporting the C interfaces directly, couldn&#x27;t Matlab link directly against that?
评论 #11042305 未加载