I think the sum examples have the problem that they're telling the user ("do this instead") to sum a range using a for loop instead of the more natural (at least in my opinion) (0..3).sum()
.
I don't think that that's a fatal problem with it, as inspect
has an example of using fold
to sum and you can replace summing with something else that doesn't have a premade iterator method.
And I'm sure there are more like that, I just opened the Iterator
docs because there are a lot of methods and ctrl+f'd println
.
Yes, but here are two more examples of where println!
doesn't help:
#![feature(iter_collect_into)]
let a = [1, 2, 3];
let mut vec: Vec::<i32> = Vec::with_capacity(6);
a.iter().map(|&x| x * 2).collect_into(&mut vec);
a.iter().map(|&x| x * 10).collect_into(&mut vec);
assert_eq!(6, vec.capacity());
println!("{:?}", vec);
There's no reason to have a println!
when assert_eq!
effectively prints the result into the documentation.
let a = [1, 2, 3];
let mut vec: Vec::<i32> = Vec::with_capacity(6);
let count = a.iter().collect_into(&mut vec).iter().count();
assert_eq!(count, vec.len());
println!("Vec len is {}", count);
let count = a.iter().collect_into(&mut vec).iter().count();
assert_eq!(count, vec.len());
println!("Vec len now is {}", count);
This also has the same problem. Replacing the last line with assert_eq!(count, 6);
or assert_eq!(vec.len(), 6);
would be a better explanation of what's going. It has the same benefit as the println!
, but the result is included in the documentation rather than requiring you to run the code.
inspect is another good example where I think println
is very natural and probably matches how the average programmer would use the method.
Surely at least println
's own docs should be able to use println
?
Yes, but this would probably be done by a lint, and that lint could deny
by default for the standard library and allow
for println!
's own documentation or for places where println!
is what's used in the vast majority of use cases for some function.