Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Gozala opened this issue Jan 10, 2017 · 0 comments
Open

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

Gozala opened this issue Jan 10, 2017 · 0 comments

Comments

@Gozala
Copy link

Gozala commented Jan 10, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant