Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export function only to module test?

Tags:

rust

I use the standard way (according to the Rust book) to write unit tests:

fn func() -> i32 {
    0
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn func_test() {
        let res = func();
    }
}

to make it compile, I have to make func public via the pub keyword.

Is it possible to make func private, but use it inside the inner test module?

like image 214
user1244932 Avatar asked Jul 03 '16 10:07

user1244932


People also ask

Can you only have one function named export?

You can have multiple named exports per module but only one default export. Each type corresponds to one of the above syntax. After the export keyword, you can use let , const , and var declarations, as well as function or class declarations.

What is export and module export?

The module is a plain JavaScript Object representing the current module. It is local to each module and also it is private. It has exports property which is a plain JavaScript variable, set to module. exports. At the end of the file, Node.

Can you export a function?

You can export as many functions as needed as long as you remember that there can be only one default export. The default export in JavaScript is used to export a single/fallback value from a module.

What is module export?

Module exports are the instructions that tell Node. js which bits of code (functions, objects, strings, etc.) to export from a given file so that other files are allowed to access the exported code.


1 Answers

As of Rust 1.15 your code works without problems!

Previously only public symbols were imported via a wildcard-import (like use super::*;). This behavior changed as specified in RFC 1560. You can see my full previous answer in the edit logs.

like image 56
Lukas Kalbertodt Avatar answered Oct 03 '22 03:10

Lukas Kalbertodt