How to move tests into a separate file for binaries in Rust's Cargo?

前端 未结 3 566
无人共我
无人共我 2020-12-15 06:03

I created a new binary using Cargo:

cargo new my_binary --bin

A function in my_binary/src/main.rs can be used for a test:

3条回答
  •  醉梦人生
    2020-12-15 06:41

    You're right; function_from_main is inaccessible outside of main.rs.

    You need to create an src/lib.rs and move the functions you want to test piecemeal. Then you'll be able to use extern crate my_binary; from your test module, and have your functions appear under the my_binary namespace.

提交回复
热议问题