/* ---[ Cheat sheet for Rust namespaces ]--- */
Rust namespaces can be a little mind-bending. This brief blog post is meant to provide instruction by example on setting up an application or library of Rust code that uses multiple files in a hierarchical namespace.
Though the current docs-project-structure guide on the Rust wiki is pretty sparse, you should start there. Then read the section on Crates and Modules in the tutorial.
I used those references plus an examination of how files and namespaces in the libstd code tree are structured to come up with this example.
In this simple example, I want to have an application where things are namespace to the top level module abc
. I want to have a couple of files (namespaces) under abc
, some additional directories (namespaces) below abc
, such as abc::xyz
and have modules in the abc::xyz
namespace. Furthermore, I want all those namespaces to be able to refer to each - both down the chain and up the chain.
Here is a simple example that illustrates how to do it. I am using Rust-0.10pre (built 16-Mar-2014).
First, I have a project I called "space-manatee", under which I have a src
directory and then my code hierarchy starts:
quux00:~/rustlang/space-manatee/src$ tree
.
├── abc
│ ├── mod.rs
│ ├── thing.rs
│ ├── wibble.rs
│ └── xyz
│ ├── mod.rs
│ └── waldo.rs
└── main.rs
2 directories, 6 files
To provide a namespace foo
in Rust, you can either create a file called foo.rs
or a dir/file combo of foo/mod.rs
. The content of my abc/mod.rs
is:
quux00:~/rustlang/space-manatee/src$ cat abc/mod.rs
pub mod thing;
pub mod wibble;
pub mod xyz;
All this module does is export other modules in the same directory. It could have additional code in it - functions and data structures, but I elected not to do that.
xyz
is a directory, and since I created the xyz/mod.rs
dir/file combo, it is a namespace that can be used and exported.
Let's look into the other files:
quux00:~/rustlang/space-manatee/src$ cat abc/thing.rs
extern crate time;
use time::Tm;
pub struct Thing1 {
name: ~str,
when: time::Tm,
}
pub fn new_thing1(name: ~str) -> ~Thing1 {
~Thing1{name: name, when: time::now()}
}
thing.rs
pulls in the rustlang time
crate and then defines a struct and constructor for it. It doesn't reference any other space-manatee code.
quux00:~/rustlang/space-manatee/src$ cat abc/wibble.rs
use abc::thing;
use abc::thing::Thing1;
pub struct Wibble {
mything: ~Thing1
}
pub fn make_wibble() -> ~Wibble {
~Wibble{mything: thing::new_thing1(~"cthulu")}
}
wibble.rs
, however, does reference other space-manatee projects, so it uses the fully qualified namespace from the top of the hierarchy, but it does not have to explicitly "import" anything. It can find the thing
namespace without a mod
declaration because thing.rs
is in the same directory.
OK, let's look into the xyz
directory now.
quux00:~/rustlang/space-manatee/src$ cat abc/xyz/mod.rs
pub mod waldo;
That just exports the waldo namespace in the same directory. What's in waldo?
quux00:~/rustlang/space-manatee/src$ cat abc/xyz/waldo.rs
use abc::wibble::Wibble;
pub struct Waldo {
magic_number: int,
w: ~Wibble
}
The Waldo struct references the Wibble struct that is higher than it in the hierarchy. Notice there is no "import" via a mod
statement - apparently going up the hierarchy requires no import.
So that's the supporting cast. Let's see how the main.rs program uses them:
quux00:~/rustlang/space-manatee/src$ cat main.rs
extern crate time;
use abc::{thing,wibble};
use abc::thing::Thing1;
use abc::wibble::Wibble;
use abc::xyz::waldo::Waldo;
pub mod abc;
fn main() {
let tg: ~Thing1 = thing::new_thing1(~"fred");
println!("{}", tg.name);
let wb: ~Wibble = wibble::make_wibble();
println!("{}", wb.mything.name);
let wdo = Waldo{magic_number: 42,
w: wibble::make_wibble()};
println!("{:?}", wdo);
}
The only mod
"import" main.rs
had to do is of the abc
namespace - which is in the same directory as main.rs. In fact, that is all you can import. If you try mod abc::thing
the compiler will tell you that you aren't doing it right.
By importing abc
, you are importing abc/mod.rs
. Go back up and look at what abc/mod.rs
does - it imports other modules, which in turn import other modules, so they all end up being imported into main.rs
as addressable entities.
Once all those import references are set up, nothing special has to be done to compile and run it:
quux00:~/rustlang/space-manatee/src$ rustc main.rs
quux00:~/rustlang/space-manatee/src$ ./main
fred
cthulu
abc::xyz::waldo::Waldo{
magic_number: 42,
w: ~abc::wibble::Wibble{
mything: ~abc::thing::Thing1{
name: ~"cthulu",
when: time::Tm{tm_sec: 21i32,
tm_min: 26i32, tm_hour: 21i32, tm_mday: 17i32, tm_mon: 2i32,
tm_year: 114i32, tm_wday: 1i32, tm_yday: 75i32, tm_isdst: 1i32,
tm_gmtoff: -14400i32, tm_zone: ~"EDT", tm_nsec: 663891679i32
}
}
}
}
(I hand formatted the Waldo output for easier reading.)
Thanks. Was helpful. Though I use slightly different module structure (foo.rs as opposed to foo/mod.rs), it was helpful nonetheless. :)
ReplyDeleteGreat Article IoT Projects for Students
DeleteDeep Learning Projects for Final Year
JavaScript Training in Chennai
JavaScript Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
ReplyDeleteThank you so much for sharing such a valuable information
best sap simple finance online training institute in hyderabad
jika anda lebih sering mengalami kekalahan daripada kemenangan, sebab setiap pemain judi ingin lebih sering bisa menikmati hasil kemenangan daripada meratapi uang mereka yang hilang karena kalah taruhan.
ReplyDeleteasikqq
http://dewaqqq.club/
http://sumoqq.today/
interqq
pionpoker
bandar ceme terbaik
betgratis
paito warna terlengkap
forum prediksi
Nice Information
ReplyDeleteYaaron Studios is one of the rapidly growing editing studios in Hyderabad. We are the best Video Editing services in Hyderabad. We provides best graphic works like logo reveals, corporate presentation Etc. And also we gives the best Outdoor/Indoor shoots and Ad Making services.
video editing studios in hyderabad
short film editors in hyderabad
corporate video editing studio in hyderabad
ad making company in hyderabad
Good Information
ReplyDelete"Sanjary Academy provides excellent training for Piping design course. Best Piping Design Training Institute in Hyderabad,
Telangana. We have offer professional Engineering Course like Piping Design Course,QA / QC Course,document Controller
course,pressure Vessel Design Course, Welding Inspector Course, Quality Management Course, #Safety officer course."
Piping Design Course in India
Piping Design Course in Hyderabad
Piping Design Course in Hyderabad
QA / QC Course
QA / QC Course in india
QA / QC Course in Hyderabad
Document Controller course
Pressure Vessel Design Course
Welding Inspector Course
Quality Management Course
Quality Management Course in india
Safety officer course
nice
ReplyDeletebrunei darussalam hosting
inplant training in chennai
good....!
ReplyDeletedominican republic web hosting
iran hosting
palestinian territory web hosting
panama web hosting
syria hosting
services hosting
afghanistan shared web hosting
andorra web hosting
belarus web hosting
Very nice blog and articles.
ReplyDeleteInternship providing companies in chennai | Where to do internship | internship opportunities in Chennai | internship offer letter | What internship should i do | How internship works | how many internships should i do ? | internship and inplant training difference | internship guidelines for students | why internship is necessary
💰Get free 250$ on Ysense trusted network without investment
ReplyDelete📢 Join Crypto quantum leap world's famous online Crypto course
🦷 Read Secret teeth care tips
About
Teeth whitening,
Teeth decay,
Teethsche,
Articlepins.com brings latest news, Trending news, Today news, General knowledge, Programming tutorials etc. >> Click Here To Read Articlepins.com Here
ReplyDelete