Skip to content
TheChatArchiveThe Chat Archive
o3-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

fn addtask(&mut self, description:String){ let task = Task{ id: self.nextid,…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Metallama-4-maverick-17b-128e-instruct’s answer.
  1. User

    fn add_task(&mut self, description:String){ let task = Task{ id: self.next_id, description: description, completed:false } self.tasks.push(task); self.next_id+=1; println!("Task added : {}", description); } explain the error and how to fix it?

  2. Assistant

    description is a String.
    In Rust, String doesn’t implement the Copy trait, so whenever you assign it to another variable or field it is moved (ownership is transferred).
    In your method the move happens here:

    let task = Task {
        id: self.next_id,
        description: description,   // ← ownership of `description` moved
        completed: false,
    };
    

    After that line the original variable description is no longer valid, yet you try to use it again:

    println!("Task added : {}", description); // ← use-after-move → compile-time error
    

    Compiler wording (simplified):

    value borrowed here after move
    move occurs because `description` has type `String`, which does not implement the `Copy` trait
    

    ───────────────────────────────────────── How to fix it ─────────────────────────────────────────

    Pick the option that best fits your needs.

    1. Borrow (&str) instead of own (String)
      – Zero copies, simplest if you only read the text inside the function.
    fn add_task(&mut self, description: &str) {
        let task = Task {
            id: self.next_id,
            description: description.to_string(), // allocate String here
            completed: false,
        };
        self.tasks.push(task);
        self.next_id += 1;
        println!("Task added: {}", description); // still valid: we only borrowed it
    }
    
    1. Clone before you move
      – Keeps the signature unchanged but duplicates the string.
    fn add_task(&mut self, description: String) {
        let printable = description.clone();          // make a copy
        let task = Task {
            id: self.next_id,
            description,                              // move original into Task
            completed: false,
        };
        self.tasks.push(task);
        self.next_id += 1;
        println!("Task added: {}", printable);        // use the clone
    }
    
    1. Print after the task is created (use the field you just stored)
      – No extra clone, but you look the value up from task (or self.tasks).
    fn add_task(&mut self, description: String) {
        let task = Task {
            id: self.next_id,
            description,          // moved, fine
            completed: false,
        };
        println!("Task added: {}", task.description); // borrow from the struct
        self.tasks.push(task);
        self.next_id += 1;
    }
    

    Any of these three removes the “borrow of moved value” error.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.