Skip to content

Point out why string values aren't moved into println! #15

Description

@Gozala

As I was going through the ownership tutorial and then an associated exercise, I wound up with a following solution:

fn print_out(name: String) {
    let (name, devowelized_name) = remove_vowels(name);
    println!("Removing vowels yields {:?}", devowelized_name);

    // Goal #2: What happens when you uncomment the `println` below?
    // Can you change the code above so that the code below compiles
    // successfully?
    //
    let (name, devowelized_name) = remove_vowels(name);
    println!("Removing vowels from {:?} yields {:?}",
             name, devowelized_name);

    // Extra credit: Can you do it without copying any data?
    // (Using only ownership transfer)
}

and later was surprised to find out that second call to remove_vowels was unnecessary & devowelized_name could have being passed to println! twice. My (not validated) guess is that macros don't take ownership unlike functions, but I think it's worth explaining in the tutorial.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions