[英]How do a pass a database variable to a function in Rust?
I'm just starting to look at Rust. 我刚刚开始看Rust。 I wanted to experiment with a database, and found the sqlite repo which is good to have to experiment with.
我想尝试一个数据库,并找到了sqlite repo,这是一个很好的必须进行实验。
I would like to know the "correct" way to pass the sqlite database variable to a function. 我想知道将sqlite数据库变量传递给函数的“正确”方法。 The error messages that I was initially getting from the compiler appeared to indicate that when I passed the Db variable from main() to the function, it was gone, so I returned it.
我最初从编译器获得的错误消息似乎表明,当我从main()传递Db变量到函数时,它已经消失,所以我返回了它。 Although this appears to work, it doesn't seem to me that it would be the normal way.
虽然这似乎有效,但在我看来这不是正常的方式。 While I'm not a believer in a large number of Global variables, I attempted to create a Global variables, but I couldn't discover how to do that.
虽然我不是大量全局变量的信徒,但我试图创建一个全局变量,但我无法发现如何做到这一点。
Below is the test program. 以下是测试程序。 Please note that I am not yet using the Rust naming conventions, but it is very-early days
请注意,我还没有使用Rust命名约定,但现在还很早
The main lines in question are : 主要问题是:
oDb1 = fCreateTable(oDb1);
fn fCreateTable(oDb1:sqlite::database::Database) -> sqlite::database::Database {
and what is the alternative and why is it necessary (in this instance) to return it? 什么是替代方案,为什么有必要(在这种情况下)返回它?
Example program: 示例程序:
extern mod sqlite;
fn main() {
let mut oDb1:sqlite::database::Database;
oDb1 = fOpenDb();
oDb1 = fCreateTable(oDb1) ;
let mut iInsertTot: int = 0;
while iInsertTot < 25 {
let oDbExec = oDb1.exec("INSERT INTO test (sname, iborn) VALUES ('xxxxx', 1973)");
if (! oDbExec.is_ok()) {
fail!(fmt!("Insert Nr. %d Failed!", iInsertTot+1));
}
iInsertTot += 1;
}
println (fmt!("Inserts completed = %d", iInsertTot));
}
fn fOpenDb() -> sqlite::database::Database {
let oDbOpen = sqlite::open("test.db");
if oDbOpen.is_err() {
fail!(fmt!("Error opening test.db: %?", oDbOpen));
}
println(fmt!("Database Open OK? %?", oDbOpen.is_ok()));
oDbOpen.unwrap()
}
fn fCreateTable(oDb1:sqlite::database::Database) -> sqlite::database::Database {
let mut oDbExec = oDb1.exec("drop table if exists test");
println(fmt!("Drop Table OK? %?", oDbExec.is_ok()));
if (!oDbExec.is_ok()) {
fail!("Drop-table failed");
}
oDbExec = oDb1.exec("CREATE TABLE test (ikey INTEGER PRIMARY KEY not null,
sname text, iborn int)");
println(fmt!("Create OK? %?", oDbExec.is_ok()));
if !oDbExec.is_ok() {
fail!("Create Table failed");
}
oDb1
}
sqlite::database::Database
implements Drop
, meaning it has a destructor, meaning it is never copied and always moved: fCreateTable(oDb1)
moves the database object out of oDb1: Now there's nothing left in oDb1
! sqlite::database::Database
实现Drop
,这意味着它有一个析构函数,这意味着它不会被复制,并始终感动: fCreateTable(oDb1)
移动数据库对象进行ODB1的:现在什么都不剩在oDb1
! Of course, you can put back something. 当然,你可以放回一些东西。 For example, when you return the database from
fCreateTable
, you again move - back into fCreateTable
. 例如,当您从
fCreateTable
返回数据库时,您再次移回fCreateTable
。
But this is a silly dance. 但这是一场愚蠢的舞蹈。 Just don't move the database in the first place, borrowed a pointer to it:
只是不要首先移动数据库,借用指向它的指针:
fn main() {
let oDb1 = fOpenDb();
fCreateTable(&oDb1);
...
}
fn fCreateTable(oDb1: &sqlite::database::Database) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.