Random Data generation API

#[post("/users/create-user", format = "json", data = "<user_info>")]
pub async fn create_user(user_info: Json<Vec<UserInputUser>>) -> Value {
    let mut rng = rand::thread_rng();

    loop {
        let user_info_random = UserInputUser {
            // Generate random data using the `rand` crate
            first_name: rng.sample_iter(rand::distributions::Alphanumeric).take(10).map(char::from).collect(),
            middle_name: rng.sample_iter(rand::distributions::Alphanumeric).take(10).map(char::from).collect(),
            last_name: rng.sample_iter(rand::distributions::Alphanumeric).take(10).map(char::from).collect(),
            email: rng.sample_iter(rand::distributions::Alphanumeric).take(10).map(char::from).collect(),
            role_id: rng.sample_iter(rand::distributions::Alphanumeric).take(10).map(char::from).collect(),
            password: rng.sample_iter(rand::distributions::Alphanumeric).take(10).map(char::from).collect()

        // Insert the generated user into the database using your existing `create_user` function

        // Wait for 5 seconds before generating the next user

For above code I am getting following error

error[E0277]: a value of type std::option::Option<std::string::String> cannot be built from an iterator over elements of type char
--> src/routes/users.rs:280:98
280 | role_id: rng.sample_iter(rand::distributions::Alphanumeric).take(10).map(char::from).collect(),
| ^^^^^^^ value of type std::option::Option<std::string::String> cannot be built from std::iter::Iterator<Item=char>
= help: the trait FromIterator<char> is not implemented for std::option::Option<std::string::String>
= help: the trait FromIterator<std::option::Option<A>> is implemented for std::option::Option<V>

What changes should I do ?

Try this, please:

- role_id: rng.sample_iter(rand::distributions::Alphanumeric).take(10).map(char::from).collect(),
+ role_id: Some(rng.sample_iter(rand::distributions::Alphanumeric).take(10).map(char::from).collect()),
#[post("/users/create-user", format = "json", data = "<_user_info>")]
pub fn create_user(_user_info: Json<Vec<UserInputUser>>) -> Value {
    if _user_info.is_empty() {
        return json!({"message": "User info is empty"});

    thread::spawn(move || {
        loop {
            let mut rng = rand::thread_rng();
            let first_name = format!("user{}", rng.gen_range(0..1000));
            let middle_name = format!("middle{}", rng.gen_range(0..1000));
            let last_name = format!("last{}", rng.gen_range(0..1000));
            let email = format!("{}{}@example.com", first_name, last_name);
            let role_id = Some(format!("{}", rng.gen_range(1..10)));
            let password = format!("{}", rng.gen_range(1000..10000));
            let user_info = UserInputUser { first_name, middle_name, last_name, email, role_id, password};

    return json!({"message": "User creation thread started"});

Using this API, I want to add random data to the database.
Code runs properly but after hitting the API, I get the following

thread '<unnamed>' panicked at 'index out of bounds: the len is 0 but the index is 0

What should I do here ?

I don't believe the panic you see is raised in the snippet you presented. Normally there is a file name and a line number after your panic which should help you find the panic's source

for me its src/services/users.rs:141:23
which redirects to

let new_user: NewUser = NewUser {
        id: &user_id,
        first_name: &user_details.first_name,
        middle_name: &user_details.middle_name,
        last_name: &user_details.last_name,
        email: &user_details.email,
        role_id: &mut role[0].id,
        password: &hashed,

role_id line.

Well, if you try to index role (some empty slice or Vec I assmue) with role[0], the panic is raised, because there is no element in role. I don't know why role is empty.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.