These are chat archives for rust-lang/rust

2nd
Feb 2018
Kelly Thomas Kline
@kellytk
Feb 02 2018 05:17
Can main.rs include code, that is not a module, from other files in the same src directory? I'd like to move a few functions from main.rs into a separate file
Kelly Thomas Kline
@kellytk
Feb 02 2018 06:08
In a scenario where main.rs declares two mods, each isolated to their own directory off of src, how would mod1 use mod2 without main.rs having to make the mod declaration? What I dislike about that is it clutters main.rs with modules in the project that are shared amongst the project's modules, but not used directly by main.rs
Joonas Koivunen
@koivunej
Feb 02 2018 07:33
@kellytk in such cases i usually do a "subsystem" module that is the parent of mod1 and mod2 and aim to give a somewhat defined/simple interface between main and subsystem1 to launch the app
@kellytk as the subsystem1 begins quickly looking like a library, so I define it at src/lib.rs and use the library in src/main.rswith extern crate $cratename;
Kelly Thomas Kline
@kellytk
Feb 02 2018 07:35
@koivunej Point taken. Do you know if Rust even supports including functions from other files without shrouding them as modules?
Joonas Koivunen
@koivunej
Feb 02 2018 07:36
@kellytk the code quality in my latest cli app is not so great but it demonstrates the idea: https://github.com/koivunej/xmlreorder ... main only uses two elements from the library (fn reorder(...) and ReorderingOptionsBuilder)
@kellytk i guess there is the include!(...) (or something similar) macro but I guess the least surprising way for any future programmers stumbling on your project is the main/lib separation ... raw including is used usually (that I know of) only with pre-procedural macro code generation
Kelly Thomas Kline
@kellytk
Feb 02 2018 07:40
Thanks @koivunej
Joonas Koivunen
@koivunej
Feb 02 2018 07:41
@kellytk you're welcome!
Kelly Thomas Kline
@kellytk
Feb 02 2018 07:42
@koivunej For my second question from above, do you happen to know if a module can use another module, both directly under main.rs, without main.rs having to be involved?
I guess I should had thought about that before the include!(...) but I can't remember ever using paths like with the playground example
Kelly Thomas Kline
@kellytk
Feb 02 2018 08:00
Jonas Platte
@jplatte
Feb 02 2018 08:03
@kellytk You can't have multiple files on the playground
So having a mod declaration with no content (as in mod foo; instead of mod foo { ... }) won't work
Also to answer your previous question, no you can't have submodules without declaring them.
Kelly Thomas Kline
@kellytk
Feb 02 2018 08:04
But copied into 3 separate files locally and run, does it compile for you?
Jonas Platte
@jplatte
Feb 02 2018 08:05
It won't because there is a typo (Moda1 instead of Mod1)
Also your mod2.rs will not be compiled when it isn't declared in main.rs
Jonas Platte
@jplatte
Feb 02 2018 08:06
But since you're not using it, that shouldn't cause any issues
Jonas Platte
@jplatte
Feb 02 2018 08:08
It still won't compile, and I just checked the compiler error message, I'd say it's pretty clear..
Jonas Platte
@jplatte
Feb 02 2018 08:08
You can have mod1::mod2 but under the path src/mod2.rs, theoretically
But that would be confusing to anyone reading your code, I would recommend against that.
Kelly Thomas Kline
@kellytk
Feb 02 2018 08:09
The 2 mods should be siblings to each other, with main using mod1 and mod1 using mod2
Jonas Platte
@jplatte
Feb 02 2018 08:09
Yes, then they need to both be declared as submodules of the same module
So they need to be declared in the same file
main.rs in this case
Kelly Thomas Kline
@kellytk
Feb 02 2018 08:10
If main only uses mod1 directly, why should a mod2 declaration be present?
Jonas Platte
@jplatte
Feb 02 2018 08:11
Because that's how you declare that there is a module mod2 present under the root of your crate
There is most likely going to be a small revamp of the module system as part of the 2018 epoch (kind of like a major release, but the compiler will still support compiling code from before it)
Kelly Thomas Kline
@kellytk
Feb 02 2018 08:12
And then the root is accessed in mod1 by prepending the mod2 call with ::, correct?
Jonas Platte
@jplatte
Feb 02 2018 08:13
I'm not fully up-to-date on what the current idea is, but one proposal for the new module system involved getting rid of mod declarations of the mod foo; form altogether IIRC.
Depends
use statements are relative to the crate by default
So in mod1, you could use mod2; and then write mod2::Mod2 elsewhere in the module to refer to the struct in mod2
Or use mod2::Mod2; and refer to it as just Mod2 later.
Or you can omit the use and refer to it as ::mod2::Mod2, like you mentioned already.
Paths outside of use statements are relative to the current module by default, and to make them relative to the crate you prepend them with ::.
@kellytk Does that make sense to you?
Denis Lisov
@tanriol
Feb 02 2018 08:49
@jplatte IIRC, automatic loading of modules is not in the merged PR, as it is too controversial.
Andrey Lesnikov
@ozkriff
Feb 02 2018 08:51
Hi. Can I somehow tell rustdoc to extract doc-comments to separate .md files? I want to run markdownlint-cli on them.
Jonas Platte
@jplatte
Feb 02 2018 09:11
@tanriol I didn't know one of the module RFCs was merged
Denis Lisov
@tanriol
Feb 02 2018 09:15
rust-lang/rfcs#2126 is in, which is mostly path syntax / optional extern crate / $name.rs instead of mod.rs
Jonas Platte
@jplatte
Feb 02 2018 09:16
Yeah I found it
Looks nice :)
Doesn't address @kellytk's problem, but for me personally it seems like a pure win
The RFC text reminded me of something I think I read in the module system revamp discussion before, but then forgot again: That the crate root has an implicit extern crate std;, but submodules don't have an implicit use std;. Really ugly, and I'm going to be very happy to see that fixed :)
Kelly Thomas Kline
@kellytk
Feb 02 2018 09:26
How do we conditionally branch from whether or not a variable has been initialized? let mut x; if !x ...generates an error
Jonas Platte
@jplatte
Feb 02 2018 09:27
@kellytk You need to use Option for that.
let mut x = None;
if x.is_none() { ... }
Kelly Thomas Kline
@kellytk
Feb 02 2018 09:41
Thanks @jplatte
Kelly Thomas Kline
@kellytk
Feb 02 2018 09:58
Can the item of a for in loop be type annotated? for arg: String in args appears to generate an error
Michal 'vorner' Vaner
@vorner
Feb 02 2018 10:13
@kellytk It should be possible (you should be able to use whatever pattern there), what kind of error do you get?
Kelly Thomas Kline
@kellytk
Feb 02 2018 10:17
@vorner Unwinding my goal, do you know why the error on line 5 at https://play.rust-lang.org/?gist=a6c56bcc34091f5a70579c1fc53d66d6&version=stable is generated?
Michal 'vorner' Vaner
@vorner
Feb 02 2018 10:21
Because that Path thing lives only inside the block and you try to put a reference to it into a variable that should live outside the block?
Kelly Thomas Kline
@kellytk
Feb 02 2018 10:22
I suppose I need to give ownership of a &str from a String to initialize a new Path, is that correct?
Michal 'vorner' Vaner
@vorner
Feb 02 2018 10:23
That, but also the path itself (& takes reference to a temporary path).
Kelly Thomas Kline
@kellytk
Feb 02 2018 10:24
What should line 4 look like instead if you don't mind? I'm stuck at putting it all together
Michal 'vorner' Vaner
@vorner
Feb 02 2018 10:26
Cat you share the whole picture of the code? I'm missing the connections to the rest.
But probably something like = Some(PathBuf::new(…))
Kelly Thomas Kline
@kellytk
Feb 02 2018 10:35
@vorner https://play.rust-lang.org/?gist=dac532102ff4227618ee7d7b72c1e146&version=stable it's more. The playground has poor support for multiple file projects so it's kind of a pain to translate code into a runnable example. Tell me if you need more. The error generated is on line 10
Michal 'vorner' Vaner
@vorner
Feb 02 2018 11:14
@kellytk Yes, my previous guess seems correct… you want PathBuf, not &Path
Kelly Thomas Kline
@kellytk
Feb 02 2018 11:24
What does the code look like @vorner ?
Michal 'vorner' Vaner
@vorner
Feb 02 2018 11:25
let mut path = Option<PathBuf>;
for arg in args {
  path = Some(PathBuf::new(arg));
}
Kelly Thomas Kline
@kellytk
Feb 02 2018 11:27
PathBuf's new doesn't accept arguments
Michal 'vorner' Vaner
@vorner
Feb 02 2018 11:30
Well, I obviously didn't try the code, but according to the docs, there's bunch of From implementations for it, so PathBuf::from(arg) might work: https://doc.rust-lang.org/nightly/std/path/struct.PathBuf.html#impl-From%3CBox%3CPath%3E%3E
Kelly Thomas Kline
@kellytk
Feb 02 2018 11:33
::from provided the solution, thank you @vorner. Why was PathBuf necessary?
Michal 'vorner' Vaner
@vorner
Feb 02 2018 11:34
PathBuf owns its data, Path is similar to str as it is only a view into data someone else owns.
So this one makes a copy of the data.
Kelly Thomas Kline
@kellytk
Feb 02 2018 11:34
Thanks
Restioson
@Restioson
Feb 02 2018 12:34
@vorner whats the point of that loop
Michal 'vorner' Vaner
@vorner
Feb 02 2018 12:34
@Restioson No idea. The loop was in the original code, so I left it there.
Restioson
@Restioson
Feb 02 2018 12:34
odd
Marek Janda
@mjanda
Feb 02 2018 21:44
Hello, can you help me please? I want to write method that will accept Result<T, U> where T is any tupple struct wrapping String. Can it be done? Thank you