0

To practice crates in Rust, i decided to create a library which contains info about different species of snails, and there is a problem: i can't create a variable using let keyword. Should i place the variable into some function, or are there any ways to create a variable out of function? Or should i make functions which returns info about some species instead of variables (for example. nor fulica,but fulica()). I don't like an idea about making the species functions because Snails::species::Achatina::fulica seems more convinient than Snails::species::Achatina::fulica()

mod species{
pub struct Animal {
    Superorder: String, Order: String, Suborder:String,Superfamily:String,Family:String,Genus:String,Specie:String
}
pub struct SnailInfo{
    size:(i32,i32), general_info:String,inhabitation:String, full_name: String, zoological:Animal
}
mod Achatina{
    let fulica = SnailInfo{
        size:(12,20),general_info: "Also wnown as giant african land snail, Achatina Fulica is one of the most invasive animals in the world. It originates from the Eastern Africa, but you can find them in the Asia, North America, South America and Australia",
        inhabitation: "Africa, Asia, North America, South America, Australia and some islands", full_name: "Lissachatina fulica",zoological:{Suprorder:Eupelmonata,Order:"Stylomatophora",Suborder:"Achatina",
    Superfamily:"Achatinoidae",Family:"Achatinoidae",Genus:"Lissachatina",Specie:"Lissachatina Fulica"}
    }
    let reticulata = SnailInfo{
        size:(15,25), general_info:"Eatern African snail known for its fast speed of growing and the black head.",inhabitation:"Mostly Zanzibar",full_name:"Achatina Reticulata",
    }
}

Error: | 9 | let fulica = SnailInfo{ | ^^^ expected item

P.S. I undestand, that there are many other mistakes in this piece of code, but i'll improve them myself

  • 2
    Does this answer your question? [Is it possible to use global variables in Rust?](https://stackoverflow.com/questions/19605132/is-it-possible-to-use-global-variables-in-rust) – prehistoricpenguin Jul 05 '21 at 06:14

1 Answers1

0

If you turn all those Strings into &'static strs, you can declare your snails as statics:

mod species {
    pub struct Animal {
        Superorder: &'static str,
        Order: &'static str,
        Suborder: &'static str,
        Superfamily: &'static str,
        Family: &'static str,
        Genus: &'static str,
        Species: &'static str,
    }

    pub struct SnailInfo {
        size: (i32, i32),
        general_info: &'static str,
        inhabitation: &'static str,
        full_name: &'static str,
        zoological: Animal,
    }

    mod Achatina {
        // Note that you have to import the types from the containing module.
        use super::{Animal, SnailInfo};

        static FULICA: SnailInfo = SnailInfo{
            size: (12,20),
            general_info: "Also wnown as giant african land snail, Achatina Fulica is one of the most invasive animals in the world. It originates from the Eastern Africa, but you can find them in the Asia, North America, South America and Australia",
            inhabitation: "Africa, Asia, North America, South America, Australia and some islands",
            full_name: "Lissachatina fulica",
            zoological: Animal {
                Superorder: "Eupelmonata",
                Order: "Stylomatophora",
                Suborder: "Achatina",
                Superfamily: "Achatinoidae",
                Family: "Achatinoidae",
                Genus: "Lissachatina",
                Species: "Lissachatina Fulica",
            }
        };
    }
}

I would highly recommend you check out the rust book.

isaactfa
  • 5,461
  • 1
  • 10
  • 24