What the frick am I supposed to do? I have that in rust, the user must enter a key-value pair.

What the frick am I supposed to do?

I have that in rust, the user must enter a key-value pair.
The key is always treated as a string, while the value can be a string, a numeric value, a boolean value.

Now, since I haven't had as much time to think of a less delayed solution, I saved both the string and numeric value as a string, the latter either like this or with something like this (treat it as pseudo code):

if input.is_numeric(){
let result =parse_numeric_value(int);return result;
}

The problem is to cast the boolean value, which on user request is taken as input as 'true' or 'false' written in full. ABSOLUTE moronic STUFF! How do I save the value anyway?

A Conspiracy Theorist Is Talking Shirt $21.68

CRIME Shirt $21.68

A Conspiracy Theorist Is Talking Shirt $21.68

  1. 2 weeks ago
    Anonymous

    Nice troll

    • 2 weeks ago
      Anonymous

      Not trollin nor baiting... How the frick do i parse a string to boolean?

      • 2 weeks ago
        Anonymous

        Ps. I called the project manager where I LITERALLY told him that parsing character by character the input and then composing a string and verifying that it equals 'true' or 'false' is literally moronic

        >"anon I don't give a damn if it's moronic, contact the client and try to reason with them! I'm telling you right now that you won't get anything because they are all people that makes products for people with mental moronation. See what you can do."

  2. 2 weeks ago
    Anonymous

    In Rust, you can handle the boolean value input as a string and then convert it to a Rust boolean type. Here’s a simple way to parse a string to a boolean:

    fn parse_boolean_value(input: &str) -> Result<bool, String> {
    match input {
    "true" => Ok(true),
    "false" => Ok(false),
    _ => Err(format!("Invalid boolean value: {}", input)),
    }
    }

    This function takes a string slice as input and returns a Result type. If the input is “true” or “false”, it will return Ok(true) or Ok(false) respectively. For any other input, it will return an Err with a message indicating the input is invalid.

    When saving the value, you can use this function to convert the input string to a boolean and then store it as needed in your data structure. If you’re using a HashMap<String, String> to store all values as strings, you might consider using an enum to handle different value types, like so:

    enum Value {
    Str(String),
    Num(i32),
    Bool(bool),
    }

    // Example usage:
    let mut map = HashMap::new();
    map.insert("key1".to_string(), Value::Str("value".to_string(*~~;
    map.insert("key2".to_string(), Value::Num(42));
    map.insert("key3".to_string(), Value::Bool(true));

    This way, you can store different types of values while still using a String as the key. Remember to handle the Result from parse_boolean_value appropriately in your code, possibly with a match or if let construct to deal with the Ok and Err cases.

    • 2 weeks ago
      Anonymous

      looking for all at once is pain, either tell users to denote it somehow or force them to enter one of them at a time by selecting the input type beforehand

      might as well do .to_lowercase() to increase tolerance, gpt-kun

      • 2 weeks ago
        Anonymous

        >might as well do .to_lowercase() to increase tolerance, gpt-kun
        Can you read?
        >The problem is to cast the boolean value, which on user request is taken as input as 'true' or 'false' written in full.
        No? Then shut the frick up, moron.

        • 2 weeks ago
          Anonymous

          >>The problem is to cast the boolean value, which on user request is taken as input as 'true' or 'false' written in full.
          >No? Then shut the frick up, moron.
          the real question is, can you? because writing is clearly not your strong point, but reading back doesnt seem to be going well either

          • 2 weeks ago
            Anonymous

            And now the gulp of the seminal matter is this... are you a troony? Or are you just mentally ill?
            Either way, I accept your concession.

    • 2 weeks ago
      Anonymous

      >In Rust, you can handle the boolean value input as a string and then convert it to a Rust boolean type.
      hello got, thanks for showing us the power of RUST

  3. 2 weeks ago
    Anonymous

    Is there a way to replicate lists in rust that contain values of any type? For example, I would like to store a list that contains strings, numbers and custom classes, like:
    ["Mary", 15, 66, "Black person", true]

    • 2 weeks ago
      Anonymous

      You could use a tuple. Even better, don't do that.

    • 2 weeks ago
      Anonymous

      You can create an enum(tagged union) of all expected types. Like GPT has recommended. (best option)
      You can create a trait, implement it for all the expected types and then store pointers to them(Box<dyn Trait>).
      You can store them as a vector of Any pointers(Box<dyn Any>)

      Literally just ask chat GPT.

    • 2 weeks ago
      Anonymous

      macro_rules! dyn_vec {
      ($($x:expr),+ $(,)?) => {
      vec![[$(Box::new($x) as Box<dyn std::any::Any>),+]]
      };
      }

      let list = dyn_vec!["Mary", 15, 66, "Black person", true];

      • 2 weeks ago
        Anonymous

        How do I use it in a struct?

        • 2 weeks ago
          Anonymous

          RTFM

        • 2 weeks ago
          Anonymous

          type DynVec = Vec<Box<dyn std::any::Any>>;

          macro_rules! dyn_vec {
          ($($x:expr),+ $(,)?) => {
          vec![$(Box::new($x) as Box<dyn std::any::Any>),+]
          };
          }

          struct Foo {
          list: DynVec
          }

          let foo = Foo { list: dyn_vec!["Mary", 15, 66, "Black person", true] };

          • 2 weeks ago
            Anonymous
          • 2 weeks ago
            Anonymous

            yeah, dyn Any does not implement Display. You have to downcast it first.
            https://doc.rust-lang.org/stable/std/any/trait.Any.html#method.downcast

          • 2 weeks ago
            Anonymous

            But isn't there an easier way?

          • 2 weeks ago
            Anonymous

            no

          • 2 weeks ago
            Anonymous

            #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
            struct List<H, T> {
            head: H,
            tail: T
            }

            macro_rules! list {
            ($($t:expr),* $(,)?) => {
            list!(@ $($t),*)
            };

            (@) => { () };

            (@ $head:expr) => {
            List { head: $head, tail: () }
            };

            (@ $head:expr, $($tail:expr),+) => {
            List { head: $head, tail: list!(@ $($tail),+) }
            };
            }

            fn main() {
            let list = list!["Mary", 15, 66, "Black person", true];
            println!("{list:?}");
            }

  4. 2 weeks ago
    Anonymous

    >rust
    stopped reading right there
    the world doesnt need another unsafe community, much more so than unsafe code

Your email address will not be published. Required fields are marked *