Convenience methods wrap up existing functionality in a more convenient way. The work done by a convenience method varies widely:
std::path::Path
`std::path::Pathtype provides methods like
` type
provides methods like stat
`staton
` on Path
`Paths that simply invoke the corresponding function in
`s that simply invoke the corresponding
function in std::io::fs
`std::io::fs`.str
`strtype provides a
` type provides a
.len()
`.len()convenience method which is also expressible as
` convenience method which is also expressible as .as_bytes().len()
`.as_bytes().len(). Sometimes the conversion is more complex: the
`.
Sometimes the conversion is more complex: the str
`strmodule also provides
` module also provides
from_chars
`from_chars`, which encapsulates a simple use of iterators.&str
`&strs provide a
`s
provide a connect
`connectas well as a special case,
` as well as a special case, concat
`concat, that is expressible using
`, that is expressible
using connect
`connectwith a fixed separator of
` with a fixed separator of ""
`""`.Providing more efficient special cases. The connect
`connectand
` and concat
`concatexample also applies here: singling out
` example
also applies here: singling out concat
`concat` as a special case allows for a more
efficient implementation.
Note, however, that the connect
`connectmethod actually detects the special case internally and invokes
` method actually detects the special case
internally and invokes concat
`concat`. Usually, it is not necessary to add a public
convenience method just for efficiency gains; there should also be a
conceptual reason to add it, e.g. because it is such a common special case.
It is tempting to add convenience methods in a one-off, haphazard way as common use patterns emerge. Avoid this temptation, and instead design small, coherent sets of convenience methods that are easy to remember:
_str
`_strvariants of methods that provide a
` variants of methods that provide a str
`stroutput, instead ensure that the normal output type of methods is easily convertible to
` output,
instead ensure that the normal output type of methods is easily convertible to
str
`str`.Path
`PathAPI mentioned above includes a small selection of the most common filesystem operations that take a
` API mentioned above includes a small
selection of the most common filesystem operations that take a Path
`Path`
argument. If one convenience method strongly suggests the existence of others,
consider adding the whole group.