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 · 1 comment
Open

Chapter 4.5, final quiz, question 2 #224

xyzzycgn opened this issue Nov 11, 2024 · 1 comment

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)

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

2 participants