Advice Please on Class/Instance in Rust


#1

Hey,

Please help me with the issue I am unable to resolve.
I have a javascript/React project, with multiple instances of same Component, calling Rust, therefor Rust should be mutilple ‘instance’ with same Struct, and fuctions with differrent data. so I need Rust side of code like Object language’s class and different instances of the object.

I have hard time implement class/instances concept in Rust. I looked at one example from stdweb/hasher(https://github.com/koute/stdweb/tree/master/examples/hasher). But this example is only one instance and all data/function are global.

Please adive how should I go about to resolve it and any examples I can use.

Thanks
G


#2

I don’t know your current knowledge level of rust and maybe I misunderstood your question, but here is an example of how to create multiple instances of a rust struct:

https://play.rust-lang.org/?gist=b0db000fc500a612511dcc190a9e9ac9&version=stable&mode=debug&edition=2015

Maybe that will help, otherwise it would be helpful if you show us some of your code or describe your concrete problem in detail.


#3

Thanks. I am new and learning Rust. I know I can initiate different instances inside Rust main; but it doesnot fit the architecture I have in combination with javascript. I may change architecture to manage instances in side Rust main function if that is only option. My situation is like this: I have a number of javascript/react/components, which are same type and different data, calling corresponding Rust functions associated with same Struct, but different data passed from differrent javascript/react/components. I am using stdweb for the javascript and Rust interaction. The new instance of Rust struct must be initiated from javascript side, so are other interactions.

Thanks
G