I'm just starting out with Rust and trying to program a list with pure pointers.
The code I have so far is the following:
struct Lista<T>{
root: *mut NodoLista<T>,
nodos: u32,
}
pub struct NodoLista<T>{
pub value: T,
pub child: *mut NodeLista<T>,
}
impl<T> NodoLista<T>{
fn new(value:T)->NodoLista<T>{
NodoLista::<T>{value, child:None}
}
}
impl<T> Lista<T>{
fn new(){
Lista::<T>{root:None, nodos: 0};
}
fn len(self)->u32{
return self.nodos;
}
}
fn main(){
let lista = Lista::new();
println!("Tamaño de la lista {}", lista.len());
}
When compiling it returns the following error:
error[E0412]: cannot find type 'NodeLista' in this scope
help: a struct with a similar name exists: 'NodoLista'
How could I solve that problem?
By changing
NodeLista
toNodoLista
, the compiler is telling you that itNodeLista
DOESN'T EXIST and suggesting that you use the name of the structure that you already defined . YouNodoLista
are using itNodeLista
as the type of the second field of the same structure. I also recommend usingOption<Box<_>>
you don't have to use that muchunsafe
and the compiler will optimize it to a pointer (even std::mem::size_of will tell you that its size is the same as a pure pointer)If you insist on pointers then you might be interested in NonNull .