Rust: String Types and Generics

Hey all,

I realise I could ask this on the official Rust forums, but I have a bias to you guys.

I am not sure about the data structures I should be using for this kind of data. So, I would appreciate help with this.

  • Say you have some JSON you want to represent as a struct:
{
  "one": "value1"
  "two": 1,
  "three": [
    { "type": "A", "val": 2 }, { "type": "B", "val": 3}
  ],
}

How would be best to represent this?

Currently I have:

pub struct ExchangeInfo {
  one: String,
  two: u16,
  three: Vec<JsonType>
}

pub enum JsonType {
  TypeOne(T1),
  TypeTwo(T2)
}

type filter<T> = String::from(T); // Error 2 below

struct T1 {
  type: filter<"A">, // Error 1 below
  val: u16
}
struct T2 {
  type: filter<"B">,
  val: u16
}

Now, I am getting errors like:

constant provided when a type was expected

ambiguous associated type
use fully-qualified syntax: <std::string::String as Trait>::from

For the type property, I know the handful of types which are possible. So, I do not want to put just String.

I may be trying to force too much TS into Rust, but I do want to learn the best practice for defining these sorts of structures.

Any help is appreciated.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.