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

Chapter 4.5, final quiz, question 2 #224

Open
xyzzycgn opened this issue Nov 11, 2024 · 4 comments
Open

Chapter 4.5, final quiz, question 2 #224

xyzzycgn opened this issue Nov 11, 2024 · 4 comments

Comments

@xyzzycgn
Copy link

xyzzycgn commented Nov 11, 2024

  • I have searched open and closed issues and pull requests for duplicates, using these search terms:
    • 4.5
    • Chapter 4
    • &Vec
  • I have checked the latest main branch to see if this has already been fixed, in this file:
    • rust-book/quizzes/ch04-05-ownership-recap.toml

URL to the section(s) of the book with this problem:
https://rust-book.cs.brown.edu/ch04-05-ownership-recap.html

Description of the problem:
The answer for the question says

For haystack, the slice type &[String] can accept more inputs than &Vec<String>, so it is preferred.

IMO this statement comes "out of the void" i.e. without a satisfying reasoning. A (short) explanation why &[String] can accept more inputs than &Vec<String> would be really helpful. In case that I've missing something in the main part of the book a reference to the accordingly chapter would be sufficient.

Suggested fix:
Adding an explanation or a reference to the accordingly chapter

@KGrewal1
Copy link

Adding on to this (as was going through the book to make sure I was solid on the basics with tests), I don't believe the suggested answer is valid as is either, as it needs lifetime annotations - would end up looking like

fn find_contains<'needle, 'haystack>(haystack: &'haystack [String], needle: &'needle str) -> Vec<&'haystack String>

to be valid, and maximally correct (as the output vector should be bound to the lifetime of the haystack)

@willcrichton
Copy link
Collaborator

The primary citation for this would be this part of 4.4:

If we have a string slice, we can pass that directly. If we have a String, we can pass a slice of the String or a reference to the String. This flexibility takes advantage of deref coercions, a feature we will cover in the “Implicit Deref Coercions with Functions and Methods” section of Chapter 15.

Defining a function to take a string slice instead of a reference to a String makes our API more general and useful without losing any functionality.

I did not add lifetime parameters to these answers because that feature is not covered until later in the book.

@xyzzycgn
Copy link
Author

xyzzycgn commented Jan 1, 2025

First of all - happy new year.

Sorry, but I can't see any correlation between the chapter you cited and the question why &[String] can accept more inputs than &Vec<String>.

@KGrewal1
Copy link

KGrewal1 commented Jan 1, 2025

I think the difficulty I found without lifetime parameters but as someone who knows rust but was using the quizzes in the book to ensure knowledge was sound, was that being an incorrect answer (can see that it wouldn't compile) threw me off. Appreciate people learning through the book won't have this problem - not sure showing them a technically wrong answer is ideal but you're the expert of paedagogy so happy to defer to you.
Happy New Years All!

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

3 participants