A clear error of the lifetime in rust

I have a rust list that I want to use, however I get an error;

error: explicit lifetime bound required
numeric(Num),
        ~~~

Specified listing:

enum expr{
   numeric(Num),
   symbol(String),
}

I don’t think I understand what is borrowed here. My intention was for Num or String to have the same lifetime as the containing expression, allowing me to return them from functions.

+4
source share
1 answer

. Num , - ( Box). ; : ( ) expr enum ? , , , String, Num? , , , expr !

, - : &Num Box<Num>. , - "" , , .

. generics monomorphized, , , . , , , .

. :

enum Expr<N: Num> {
    Numeric(N),
    Symbol(String)
}

:

enum Expr<'a> {  // '
    Numeric(&'a Num + 'a),
    Symbol(String)
}

:

enum Expr {
    Numeric(Box<Num + 'static>),  // ' // I used 'static because numbers usually don't contain references inside them
    Symbol(String)
}

, . , , -.

'a

enum Expr<'a> {  // '
    Numeric(&'a Num + 'a),
    Symbol(String)
}

- . , Numeric. &'a Num + 'a - , "- , 'a , , 'a". , -, 'a : &'a, -, : Num + 'a. , , , , -, .

Box . Box<Num + 'static> - "- , , 'static". Box - . , , , , . - , Num + 'a ; 'static lifetime. , , 'static. , , .

, :

&'a SomeTrait + 'a
&'a SomeTrait + 'static
Box<SomeTrait + 'a>  // '
Box<SomeTrait + 'static>

, 'a 'b :

&'a SomeTrait + 'b

, 'b , 'a ( - , - ), &'a SomeTrait + 'a.

+8

All Articles