简体   繁体   中英

What is the purpose of the unit type in Rust?

Rust has the unit type , () , a type with a single zero-size value. The value of this unit type is also specified using () .

What is the purpose of the unit type and its value? Is it a mechanism to avoid using null (or nil ) like other languages have?

() is a value of the type () and its purpose is to be useless.

Everything in Rust is an expression, and expressions that return "nothing" actually return () . The compiler will give an error if you have a function without a return type but return something other than () anyway. For example

fn f() {
    1i32 // error: mismatched types: expected `()` but found `int`
}

There are practical uses for () too. Sometimes we don't care about a generic type, and () makes this explicit.

For example, a Result<(), String> can be used as return type for a function that either completes successfully or fails for a variety of reasons.

If you're coming from a C-like language (C, C++, Java, etc.), you can think of unit as being like void . It is the type you return when you don't want to return anything.

Type theorists will point out that unit is not like void, because unit has exactly 1 value whereas void has 0 values.

In practice, the amount of information you can store in both types is the same (0 bits), although languages that use unit tend to be nicer to work with because you can treat it as you would any other value.

You can store it in a variable, struct, collection, or anywhere else you could store a value. You can pass as an argument or return it as a result. You can create a reference to it. Etc.

So, when is that useful? Mainly when you don't care what type of value you're dealing with. It means you can write polymorphic/generic code without needing to worry about whether the value you are dealing with actually contains any information, that is, no need of a special case for whether you're storing actual data or () .

One example of where this is used is in HashSet . A HashSet<T> is actually implemented as a thin wrapper around HashMap<T, ()> . (A map from the generic type T to () )

Unit type () is used when you have code that does not return anything. from the docs

The () type, also called “unit”.

The () type has exactly one value (), and is used when there is no other meaningful value that could be returned.

1- function that is not returning anything

fn test()->() {}

2- code block that does not return anything:

// in this case type will be let x:()={codeblock}
let x={
        println!("testing code block")
    };
  • since each unit type has exactly one value, all unit type variables are equal. for example

    fn main(){ let x=(); let y=println;("test")? // this will print out true println,("Are x and y variables equal; / {}",x==y); }

3- it is commonly used in Result enum where you are not returning anything but want to write type for successful execution. example from docs:

use std::error::Error;
use std::fs::File;

fn main() -> Result<(), Box<dyn Error>> {
    let greeting_file = File::open("hello.txt")?;

    Ok(())
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM