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

add help on pattern guard #33260

Merged
merged 1 commit into from
May 11, 2016
Merged

Conversation

mrmiywj
Copy link
Contributor

@mrmiywj mrmiywj commented Apr 28, 2016

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nrc (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@mrmiywj
Copy link
Contributor Author

mrmiywj commented Apr 28, 2016

@GuillaumeGomez

@GuillaumeGomez
Copy link
Member

Thanks for the PR!

However, I don't like the way you did. :-/
Let me explain: I think the best solution would be to rewrite the whole error explanation in order to show the two ways (by insisting on the first issue and demonstrating that in some cases we can fix it just by using reference). For now, it just comes out like "in this very specific example, you can also fix it this way".

@GuillaumeGomez GuillaumeGomez assigned steveklabnik and unassigned nrc Apr 28, 2016
@mrmiywj
Copy link
Contributor Author

mrmiywj commented Apr 28, 2016

I thought providing solution is enough. Yep u r right. To emphasize the two ways, it's better to rewrite the whole explanation.
But I'm not sure what's the optimized solution to the second situation in which the value is consumed in the pattern guard. Using &self in the function? Or any other ideas?
Thank you for your attention!

@GuillaumeGomez
Copy link
Member

You can't unfortunately. I see two solutions to avoid this issue:

  • Cloning the type.
  • Setting a boolean and then, depending on this value, consume the type.

If someone else has something in mind, I'd be glad to hear it! :)

@mrmiywj
Copy link
Contributor Author

mrmiywj commented Apr 29, 2016

Thx for your reply!
I wrote two solutions to the two different situation in the issue. Is it Ok? Hmm, I'm not an experienced rust programmer, so I'm not sure these two solutions actually solve the two different errors.

@GuillaumeGomez
Copy link
Member

You forgot the third case: when you can't clone. ;)

Otherwise that's it!

@mrmiywj
Copy link
Contributor Author

mrmiywj commented Apr 29, 2016

Hmm, according to this rfc, I don't think there is any other solutions except use non-consume function when you can't clone? Like this

I'm sorry I don't understand "Setting a boolean and then, depending on this value, consume the type." I think if I call a consuming function, then the ownership is moved, so it will cause errors...

@GuillaumeGomez
Copy link
Member

The goal is to move the consuming function call outside of the closure. But I think it's just too much precise to be used. Let's not consider it.

@mrmiywj
Copy link
Contributor Author

mrmiywj commented Apr 29, 2016

Yep, it's a too-specific situation. Just the two examples are enough.

@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Apr 29, 2016

I let you handle it then! :)

@mrmiywj
Copy link
Contributor Author

mrmiywj commented Apr 30, 2016

/cc @GuillaumeGomez Hope for your advice! It's my first PR to rust!


```compile_fail
The problem above can be solve by using `ref` keyword.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem above can be solve by using ref keyword.

"The problem above can be solved by using the ref keyword."

@GuillaumeGomez
Copy link
Member

Except for the nits, it seems all good. Great job! Ping me once corrected and squashed.

@mrmiywj
Copy link
Contributor Author

mrmiywj commented May 1, 2016

/cc @GuillaumeGomez
Thx! This my first PR to rust! I hope I can contribute to it from now on!

@GuillaumeGomez
Copy link
Member

A line of your commit is longer than 80 columns. If you want to check issues like this, you can use make tidy. Once fixed, it's good to merge!

Thx! This my first PR to rust! I hope I can contribute to it from now on!

I hope so as well! :)

@mrmiywj
Copy link
Contributor Author

mrmiywj commented May 1, 2016

/cc @GuillaumeGomez
I hope this a good start!

reference when using guards or refactor the entire expression, perhaps by
putting the condition inside the body of the arm.
Though this example seems innocuous and easy to solve, the problem becomes clear
when it encounters functions which consume the value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum... Finally, this sounds better with ":". Can you change "consume the value." to "consume the value:" please?

@GuillaumeGomez
Copy link
Member

Sorry, one last nit and ready to get merged!

@Manishearth
Copy link
Member


failures:

---- Rust_Compiler_Error_Index_13 stdout ----
    <anon>:13:14: 13:15 error: cannot bind by-move into a pattern guard [E0008]
<anon>:13         Some(y) if y.clone().consume() > 0 => {}
                       ^
error: aborting due to previous error
thread 'Rust_Compiler_Error_Index_13' panicked at 'Box<Any>', /buildslave/rust-buildbot/slave/auto-linux-64-opt-rustbuild/build/src/libsyntax/errors/mod.rs:614
thread 'Rust_Compiler_Error_Index_13' panicked at 'couldn't compile the test', /buildslave/rust-buildbot/slave/auto-linux-64-opt-rustbuild/build/src/librustdoc/test.rs:276


failures:
    Rust_Compiler_Error_Index_13

test result: FAILED. 427 passed; 1 failed; 45 ignored; 0 measured

bors added a commit that referenced this pull request May 2, 2016
Rollup of 14 pull requests

- Successful merges: #32756, #33129, #33225, #33260, #33309, #33320, #33323, #33324, #33325, #33330, #33332, #33334, #33335, #33346
- Failed merges:
fix too long column

fix typo of help on pattern guard

one nit

fix compile fail
@mrmiywj
Copy link
Contributor Author

mrmiywj commented May 3, 2016

/cc @GuillaumeGomez @steveklabnik
My bad. I'm too careless. Fix it now.

bors added a commit that referenced this pull request May 3, 2016
Rollup of 14 pull requests

- Successful merges: #32756, #33129, #33225, #33260, #33309, #33320, #33323, #33324, #33325, #33330, #33332, #33334, #33335, #33346
- Failed merges:
@GuillaumeGomez
Copy link
Member

@bors: rollup

@steveklabnik
Copy link
Member

@bors: r=guillaumegomez rollup

@bors
Copy link
Contributor

bors commented May 10, 2016

📌 Commit 201d9ed has been approved by guillaumegomez

@steveklabnik
Copy link
Member

@mrmiywj sorry that this one slipped through the cracks!

steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 10, 2016
steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 10, 2016
steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 11, 2016
bors added a commit that referenced this pull request May 11, 2016
Rollup of 9 pull requests

- Successful merges: #33129, #33260, #33345, #33386, #33522, #33524, #33528, #33539, #33542
- Failed merges: #33342, #33475, #33517
@bors bors merged commit 201d9ed into rust-lang:master May 11, 2016
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

Successfully merging this pull request may close these issues.

7 participants