rust

How do I access assets included in a Rust/Cargo project installed via `cargo install`?

淺唱寂寞╮ 提交于 2020-01-13 08:00:48
问题 I have a project which includes some associated assets (Lua scripts), which I need to find at runtime. This can mean two things: During development (e.g. cargo run ), I want to find it relative to the source When installed via cargo install , the assets should be installed somewhere as well, and the installed version of the executable should find the installed assets. I know about the option to use something like include_str!() to compile text files into the binary, but I don't want to do

lua中使用rust代码

。_饼干妹妹 提交于 2020-01-13 07:08:05
lua中使用rust代码 原文: https://blog.csdn.net/weixin_44259356/article/details/103816756 参考链接: https://my.oschina.net/u/3703365/blog/3082160 参考链接中有一点不同,可能是版本太老,我生成的是dll文件。 cargo.toml [lib] name = “double_input” crate-type = [“staticlib”, “cdylib”] lib.rs #![crate_type = “dylib”] #[no_mangle] pub extern fn double_input(input: i32) -> i32 { input * 2 } 编译rust cargo build --release lua代码 print ( "Hello World" ) ffi = require ( "ffi" ) ffi.cdef [ [ int32_t double_input ( int32_t n ) ; ] ] rust_lib = ffi.load ( "double_input.dll" ) n = 1234567 print ( rust_lib.double_input ( n )) 注:这里使用的dll文件为编译好的rust项目生成。

Passing a Rust variable to a C function that expects to be able to modify it

这一生的挚爱 提交于 2020-01-12 09:14:09
问题 I'm writing a safe Rust layer with which I can call functions from a C library in Rust. I've generated the unsafe bindings using rust-bindgen, but I'm getting a little confused on the differences between how Rust and C work with regards to passing pointers. The C function looks like this: bool imeGet(unsigned char address, int *value); It reads an I2C sensor at address , stores the result in value , and returns TRUE on success. Bindgen has the Rust function looking like this: pub fn imeGet

WebAssembly InstantiateStreaming Wrong MIME type

故事扮演 提交于 2020-01-12 07:48:07
问题 I am attempting to get this tutorial (here: https://www.hellorust.com/demos/add/index.html) to work, and it seems that whatever I do, I cannot get the WebAssembly MDN reserved function to properly work. So, I followed the instructions on the link above and got an add.wasm file. As far as I can tell this should be fairly simple and should work. After a little digging I found that the newest WebAssembly module is to instantiate streaming - the documentation for which can be found here: (https:/

How to use Fn traits/closures in signatures in Rust

淺唱寂寞╮ 提交于 2020-01-12 07:09:32
问题 I want to write an int -returning function that accepts a closure taking zero arguments, a closure taking one argument, and a closure taking two arguments, where all closure arguments are of type int and every closure returns f32 . What is that function's signature going to look like? Now I want to accept via the Fn and FnMut traits. What does the signature look like? Is the use of features in the crate required? If so, which ones and why? If known: what does it look like sugared? Desugared?

How to use Fn traits/closures in signatures in Rust

别等时光非礼了梦想. 提交于 2020-01-12 07:09:28
问题 I want to write an int -returning function that accepts a closure taking zero arguments, a closure taking one argument, and a closure taking two arguments, where all closure arguments are of type int and every closure returns f32 . What is that function's signature going to look like? Now I want to accept via the Fn and FnMut traits. What does the signature look like? Is the use of features in the crate required? If so, which ones and why? If known: what does it look like sugared? Desugared?

Using $crate in Rust's procedural macros?

…衆ロ難τιáo~ 提交于 2020-01-12 04:10:35
问题 I know what the $crate variable is, but as far as I can tell, it can't be used inside procedural macros. Is there another way to achieve a similar effect? I have an example that roughly requires me to write something like this using quote and nightly Rust quote!( struct Foo { bar: [SomeTrait;#len] } ) I need to make sure SomeTrait is in scope ( #len is referencing an integer outside the scope of the snippet). I am using procedural macros 2.0 on nightly using quote and syn because proc-macro

Is there a method like JavaScript's substr in Rust?

℡╲_俬逩灬. 提交于 2020-01-12 04:10:11
问题 I looked at the Rust docs for String but I can't find a way to extract a substring. Is there a method like JavaScript's substr in Rust? If not, how would you implement it? str.substr(start[, length]) The closest is probably slice_unchecked but it uses byte offsets instead of character indexes and is marked unsafe . 回答1: For characters, you can use s.chars().skip(pos).take(len) : fn main() { let s = "Hello, world!"; let ss: String = s.chars().skip(7).take(5).collect(); println!("{}", ss); }

How to manually return a Result<(), Box<dyn Error>>?

こ雲淡風輕ζ 提交于 2020-01-12 03:39:45
问题 I want to return an error from a function in case a condition is true: use std::error::Error; pub fn run() -> Result<(), Box<dyn Error>> { // -- snip --- if condition { // return error } // -- snip -- Ok(()) } fn main() {} I probably don't have the basics of the typesystem down, but everywhere I looked people use the ? operator, so I can't figure out what type to return. Is it possible to just return an error like this? Is there a better way to handle this logic? 回答1: Error is a trait and you

How to read a file with JavaScript to WebAssembly using Rust?

£可爱£侵袭症+ 提交于 2020-01-11 18:49:11
问题 How can I pass a File to be read within the WebAssembly memory context? Reading a file in the browser with JavaScript is easy: <input class="file-selector" type="file" id="files" name="files[]" /> I was able to bootstrap WebAssembly code written in Rust with the crate stdweb, add an event listener to the DOM element and fire up a FileReader: let reader = FileReader::new(); let file_input_element: InputElement = document().query_selector(".file-selector").unwrap().unwrap().try_into().unwrap();