<()> what does this mean?

use std::fmt;
use std::io;

fn function1() -> fmt::Result {
    // --snip--

fn function2() -> io::Result<()> { // What does this mean?
    // --snip--

io::Result<()> Not exactly sure what this is?

std::result::Result has two type parameters: Result<T, E>, where T is a "success" value and E is an "error" value.

It's customary do define module-level type alias which fixes error. For example, std::io does this:

type Result<T> = std::result::Result<T, std::io::Error>

Additionally, () is a unit-type: a type with a single value, which is spelled () as well. It is used for the same purpose as void in C-like languages.

So, io::Result<()> is a result of an operation which may fail with io::Error, but doesn't return any useful value. That is, the operation is interesting solely for side-effects.

std::fmt::Result breaks this pattern a bit: it's a type alias that fixes both T and E types:

type Result = std::result::Result<(), std::fmt::Error>;

Kind of like void. Just means "nothing". i.e. Ok(()) means success but no result.

This is actually a tuple. For example, (i32, i32) is a tuple that contains two integer, so () is a tuple that contains nothing.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.