I'm about to run into the wall to end the pain - how does Rust finds its dependencies? How does it use module namespaces? I simply don't understand, and the compiler is just being cryptic, and the documentation is... ehm... making assumptions about what it is I'd like to do, not explaining the plumbing. Can't be that difficult. And not everybody wants to write a helloworld.
So, I want to make a library and I have a lib.rs (using cargo new --lib), and I have a few files in the same directory that I want to include (called something.rs). The concept is that lib.rs contains all the access methods, and the files sitting in the same directory, implement not directly accessible methods and helper structures. That should be as simple as 'use something' right? Nope. Compiler says no (I use 'cargo build'): 'Unresolved import'. 'No something in the root'. I've tried with and without 'crate::'. I've tried 'using' the secondary file, and then the individual modules / types in the file. I've tried subdirs of the same name. I've tried putting 'mod' around my code with the same name as the file, and with the same name as the module name I'm wanting to have. Nothing. It's frustrating to say the least and when I scan the internet for the error messages I'm getting, I'm clearly not very alone.
Is there a way to make the compiler be a bit more verbose. For example, give me an idea of how it's building up an internal idea of namespacing, or what its search paths are, for example?
Example:
#!/bin/sh
set -e
rm -rf /tmp/foo
cd /tmp
cargo new foo --lib
#find /tmp/foo
echo "
use bar;
pub fn foobar
()
{
barfoo();
}
" >> /tmp/foo/src/lib.rs
echo "
fn barfoo
()
{
}
" > /tmp/foo/src/bar.rs
cd /tmp/foo
cargo build
Knowledge level increased, but now the compiler wants me to create a subdirectory, which I don't understand:
#!/bin/sh
set -e
rm -rf /tmp/foo
cd /tmp
cargo new foo --lib
echo "
mod bar;
use bar::barfoo;
pub fn foobar
()
{
barfoo();
}
" >> /tmp/foo/src/lib.rs
echo "
mod barfu;
use barfu::something;
pub fn barfoo
()
{
something();
}
" > /tmp/foo/src/bar.rs
echo "
mod bar;
use bar::barfoo;
pub fn something
()
{
barfoo();
}
" > /tmp/foo/src/barfu.rs
cd /tmp/foo
cargo build