rust-lang / rust-lang/rust-by-example
Necessity of ? operator is not clear in List testcase example
Nobody has claimed this yet.
- Dominant language
- Handlebars
- Stars
- 8.1k
- Forks
- 1.6k
- PR merge metrics
- No merged PRs in 30d
Description
This example is introducing the ? operator. Its use in the example does not appear necessary. The example compiles and runs even without using ?. It will also work correctly with an empty list, as illustrated below.
impl fmt::Display for List {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Extract the value using tuple indexing,
// and create a reference to `vec`.
let vec = &self.0;
write!(f, "[");
// Iterate over `v` in `vec` while enumerating the iteration
// count in `count`.
for (count, v) in vec.iter().enumerate() {
// For every element except the first, add a comma.
// Use the ? operator to return on errors.
if count != 0 { write!(f, ", "); }
write!(f, "{}", v);
}
// Close the opened bracket and return a fmt::Result value.
write!(f, "]")
}
}
fn main() {
let v = List(vec![]);
println!("{}", v);
}
Outputs
[]
as expected.
Is introducing ? operator in this example appropriate?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read src/hello/print/print_display/testcase_list.md and compare the example with and without the ? operator, including the behavior of an empty list. Check the surrounding explanation of fmt::Result and error propagation, then update the example or its text so the operator's necessity and purpose are clear.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100