Pushing to a vector of structs results in ()

Hi all,

I'm working on a simple CLI to track and update TODOs as a way of learning Rust.
I'm very new, but this is some unexpected behaviour that has me stumped.

I want to push a new task into my Tasks items vector, but when I do so and attempt to print the result
all that's printed is ()?

//! Trying to push to the vector of a struct

#![deny(warnings)]

// Struct containing a vector of tasks
#[derive(Debug)]
struct Tasks {
    items: Vec<Task>,
}

// Individual task structs; want to add one of these to Tasks.items
#[derive(Clone, Debug)]
struct Task {
    id: String,
    field: String,
    status: String,
}


fn main() {
    // Defining existing tasks
    let mut tasks = Tasks {
    items:
        [
            Task {
                id: "todo1".to_string(),
                field: "something to do one".to_string(),
                status: "done".to_string()
            },
            Task {
                id: "todo2".to_string(),
                field: "something to do too".to_string(),
                status: "in progress".to_string()
            }
        ].to_vec()
};
    //creating a new task
    let entry = Task { id: "1".to_string(), field: "Adding a new task to the list".to_string(), status: "in progress".to_string() };
     println!("{:?}", tasks.items.push(entry)); // prints `()` and I don't understand why?
}

(Playground)

Output:

()

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.80s
     Running `target/debug/playground`

I have a feeling it's something simple but couldn't find any existing threads/documentation about it, any help is super appreciated :slightly_smiling_face: !

push returns (), so that’s what gets printed. It’s that simple! Push first, then print it afterward, and you should see stuff.

4 Likes

:sweat_smile: Thank you so much!

tasks does indeed have its new task pushed to it, I just needed to print the right thing!

 tasks.items.push(entry); // Pushes, returns ()
 println!("{:?}", tasks); //Gives me what I need
1 Like

Glad to hear it!

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