rust


How do a pass a database variable to a function in Rust?


I'm just starting to look at Rust. I wanted to experiment with a database, and found the sqlite repo which is good to have to experiment with.
I would like to know the "correct" way to pass the sqlite database variable to a function. 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. 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
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! Of course, you can put back something. For example, when you return the database from fCreateTable, you again move - back into 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) {
...
}

Related Links

Multiple impl candidate while deriving an instance in Rust
How to create a Quickcheck Arbitrary of a struct containing a reference?
Posting form data with nickel.rs works the first time, returns 404 subsequent times
How do I hold a collection of one struct in another where lifetimes are not predictable?
Why does Rust have both call by value and call by reference?
Error: borrowed value is only… reference must be valid for
In Rust how do you pass a function as a parameter?
Obtaining rustdoc for particular function or trait
Rust slice ownership outlives its scope
What's purpose of ErrorKind::__Nonexhaustive?
A set type that preserves insertion order
Rust closure type mismatches of seemingly opposing natures
How can I build multiple binaries with Cargo?
borrowed value does not live long enough in this case ( Vec<&Fn(i32) -> i32> )
Mutability issue with self and collections in Rust
Recommended way to wrap C lib initialization/destruction routine

Categories

HOME
amazon-web-services
android-espresso
knockout.js
openxml
seedstack
add-on
nsview
google-play-services
biztalk-2010
drag-and-drop
video-streaming
specifications
jpa-criteria
watch-os-3
game-maker
tiff
mailmerge
trace32
django-rq
nest-api
pickle
backtracking
kaggle
liferay-6.2
mime-types
device
plyr
beego
basic-authentication
splunk-query
jsonresult
audit
windowsiot
progid
steam-web-api
image-optimization
android-canvas
installanywhere
llvm-ir
sspi
access-denied
1wire
git-tfs
adblock
brightscript
karabiner
redisson
android-sharing
line-intersection
stateless-session-bean
dtd
kendo-editor
dex
ltrace
openerp-6
finder
boost-compute
fuzzer
visual-studio-monaco
jenkins-workflow
openseadragon
static-methods
polymerfire
gravatar
mixins
css-paged-media
supercomputers
vdm-sl
roxygen2
bootstrap-wysiwyg
prism-4
nsmutabledata
incron
directorysearcher
uno
sunos
cakephp-2.2
global-scope
storing-data
mute
acitree
web-frameworks
mongo-jackson-mapper
ntlmv2
simplecov
story
voice-recording
server-name
nsfont
algol68
netstream
microblogging
zend-server-ce
digiflow
eaccelerator
explicit
ixmldomdocument
eventaggregator
fuzzy-comparison
gtktextview
levels

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App