rust

Deserialize variable meta object with serde

十年热恋 提交于 2021-01-28 12:21:44
问题 I am looking an elegant way to deserialize the following input: { "products": [ { "id": 1, "ptype": "Clothes", "description": "some data about clothes", "metadata": { "colors" : ["blue", "green"], "web": false, "size": 2 } }, { "id": 4, "ptype": "Food", "description": "text for foods", "metadata": { "country": "France", "wine": true } }, { "id": 12, "ptype": "EmptyPlaceholder", "description": "nothing at all", "metadata": { } } ] } The json contains an array of products. A product can be

Casting a borrowed reference with a lifetime to a raw pointer in Rust

不打扰是莪最后的温柔 提交于 2021-01-28 11:51:24
问题 I am new to rust and am trying to wrap my head around lifetimes. Please consider the following code: use jni::JNIEnv; pub struct CameraAppEngine<'a> { _env: &'a JNIEnv<'a>, _width: i32, _height: i32 } impl<'a> CameraAppEngine<'a> { pub fn new(_env: &'a JNIEnv<'a>, _width: i32, _height: i32) -> CameraAppEngine { CameraAppEngine { _env, _width, _height } } pub fn env(&'a self) -> JNIEnv<'a> { JNIEnv::from_raw(self._env).unwrap() // error! } } The JNIEnv::from_raw method has a type signature of

How to return a instance of a struct that uses a locally declared variable [duplicate]

↘锁芯ラ 提交于 2021-01-28 11:10:28
问题 This question already has answers here : Is there any way to return a reference to a variable created in a function? (4 answers) Closed 3 years ago . I am trying to figure out how to declare a variable locally and use it in a value that is being returned. The following is the code that is causing the problem use std::io; use std::string::String; use std::io::Write; // Used for flush implicitly use topping::Topping; pub fn read_line(stdin: io::Stdin, prompt: &str) -> String { print!("{}",

Private inner module returning private item gives “private type in public interface” error

拥有回忆 提交于 2021-01-28 09:12:55
问题 In the below example, the module outer has a private type Private and a private inner module inner . inner is able to access Private (because child modules can access their parent's private items , even if they are not parked as public). inner defines a function not_really_public_interface() . While it is marked as public, it is really only available to outer because inner itself is not public. outer.rs struct Private; mod inner { use super::Private; pub fn not_really_public_interface() ->

Private inner module returning private item gives “private type in public interface” error

坚强是说给别人听的谎言 提交于 2021-01-28 09:12:03
问题 In the below example, the module outer has a private type Private and a private inner module inner . inner is able to access Private (because child modules can access their parent's private items , even if they are not parked as public). inner defines a function not_really_public_interface() . While it is marked as public, it is really only available to outer because inner itself is not public. outer.rs struct Private; mod inner { use super::Private; pub fn not_really_public_interface() ->

How do I return an iterator over a 2D array with the enumeration indices included?

做~自己de王妃 提交于 2021-01-28 08:54:57
问题 I have a struct containing a 2D array: struct Block; struct World { blocks: [[Block; 10]; 10], } How could I write a function which returns an iterator over a 2D array, but with the enumeration indices included? fn enumerate_blocks(&self) -> impl Iterator<Item = (usize, usize, &Block)> I managed to write an implementation of the function which just returns an iterator without enumeration indices: fn blocks(&self) -> impl Iterator<Item = &Block> { self.blocks.iter().flat_map(|x| x.iter()) } If

How do I return an iterator over a 2D array with the enumeration indices included?

百般思念 提交于 2021-01-28 08:40:55
问题 I have a struct containing a 2D array: struct Block; struct World { blocks: [[Block; 10]; 10], } How could I write a function which returns an iterator over a 2D array, but with the enumeration indices included? fn enumerate_blocks(&self) -> impl Iterator<Item = (usize, usize, &Block)> I managed to write an implementation of the function which just returns an iterator without enumeration indices: fn blocks(&self) -> impl Iterator<Item = &Block> { self.blocks.iter().flat_map(|x| x.iter()) } If

Should Rust implementations of From/TryFrom target references or values?

佐手、 提交于 2021-01-28 08:25:37
问题 Should I be writing: impl<'a> From<&'a Type> for OtherType Or should it be impl From<Type> for OtherType I'm having a difficult time finding the answer, perhaps due to a vocabulary failure on my part. I really don't particularly care about the reference-ness/value-ness of the argument. In C++, I would define the function over/method on values and calling it on const references. Is there an automatic derivation from impl Trait<Type> to impl<'a> Trait<&'a Type> ? 回答1: Should Rust

Understanding for loop semantics when iterating through a vector containing mutable references

元气小坏坏 提交于 2021-01-28 08:16:42
问题 I am trying to understand why the following code fails: fn main() { let mut a = 10; let mut b = 20; let mut c = 30; let p = vec![&mut a, &mut b, &mut c]; // works with [&a, &b, &c] for &x in &p { // works with 'x' instead of of '&x' println!("{}", x); } } The error message is: error[E0507]: cannot move out of borrowed content --> src/main.rs:7:9 | 7 | for &x in &p { | ^- | || | |hint: to prevent move, use `ref x` or `ref mut x` | cannot move out of borrowed content As I understand, the

Why can't I read this HashMap in a NEAR contract?

∥☆過路亽.° 提交于 2021-01-28 07:50:34
问题 I have a NEAR smart contract that keeps a HashMap of Veggie records. My most basic accessor method get_veggie(vid), which looks up and returns one Veggie record, passes unit tests but fails in the deployed contract. It panics with 'veggie does not exist' when I send one of the keys returned by another accessor method, get_veggie_keys(). // this method returns an array of u64 keys: pub fn get_veggie_keys(&self) -> Vec<TokenId> { self.veggies.keys().cloned().collect() } // but this method