How to split file? or is it Recommend?
shinriyo opened this issue · comments
Hi there.
I always document write source code in main.rs
.
original file tree
- src/main.rc
So, I split program the program in main.rs
let mut server = Nickel::new();
let mut router = Nickel::router();
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
let shared_connection = Arc::new(Mutex::<Connection>::new(conn));
{
let conn = shared_connection.clone();
router.get("/api/movies", middleware! { |_, mut response|
let conn = conn.lock().unwrap();
// Do something
my purpose
path tree
- src/main.rc
- src/movie/mod.rs
mod movie; // module reading
fn main() {
let mut server = Nickel::new();
let mut router = Nickel::router();
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
movie::url(conn, router);
and
pub fn url(conn:Connection, mut router:Router) {
let shared_connection = Arc::new(Mutex::<Connection>::new(conn));
{
let conn = shared_connection.clone();
router.get("/api/movies", middleware! { |_, mut response|
However, the error occurred.
use of moved value: `router`
splitting source code does not recommend?
I just want to set argument in url method.
Could you help me?
Hi @shinriyo,
I think the signature you're looking for is:
pub fn url(shared_connection: Arc<Mutex<Connection>>, router: &mut Router) {
router.get("/api/movies", middleware! { |_, mut response|
Note the use of &mut Router
instead of Router
to prevent the 'use of moved value' error.
The usage from main.rs
would then become:
fn main() {
let mut server = Nickel::new();
let mut router = Nickel::router();
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
let shared_connection = Arc::new(Mutex::new(conn));
movie::url(shared_connection.clone(), &mut router);
Hi @Ryman
Thank you for replying.
Wow! Thank you very much! Your submit code worked with no problem!
@shinriyo Glad to hear it 👍