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

rust create a String from file.read_to_end()
Only show first screenful of compile errors in Rust when building with Cargo?
Lifetime annotations, or &str vs String
Explicit lifetime declarations in trait objects held by structs
cannot borrow `*self` as mutable because it is also borrowed as immutable
println! error: format argument must be a string literal
Is there a way to combine multiple traits in order to define a new trait? [duplicate]
Reference passed as parameter is not moved
How does context switch in Rust's green threads work?
rust - std::string::String as a byte string (i.e. b“foo” or deprecated bytes!(“foo”))
In Rust, how can I fix the error “the trait `core::kinds::Sized` is not implemented for the type `Object+'a`”
Boxed value does not live long enough
inspect rust traits at runtime
Enums with constructions in Rust
How can I unify the lifetimes between a struct and a trait?
PNaCl support for Rust

Categories

HOME
hpoo
elasticsearch
ionic2
firebase
yahoo-weather-api
systemd
compilation
braintree
ant
adobe
ios-simulator
mathematical-optimization
apache-kafka-connect
visualforce
graphql-js
yahoo
cqrs
circleci
freertos
smartgwt
global-variables
heap-dump
l20n
susy-sass
string-formatting
angular2-highcharts
tableview
substring
tracing
plyr
gravity-forms-plugin
cube
javax.persistence
envoy
objectmapper
instructions
reporting
xcrun
plantuml
directfb
bootstrap-sass
android-canvas
polyml
textmate2
cloudera-quickstart-vm
variable-variables
microsoft-ui-automation
hidden-markov-models
gitolite
selenide
imgur
robust
fluid-mac-app-engine
permission-denied
rightnow-crm
.net-micro-framework
solr-query-syntax
dtd
sidewaffle
fitbit
cefpython
ftp-client
simian
fdt
openmrs
flask-security
wepay
stack-smash
calcite
360-degrees
gql
mobilefirst-server
alwayson
ampersand
tree-structure
ocmockito
pass-by-value
fat32
dos2unix
thoughtworks-go
comctl32
cout
iplimage
object-code
nidaqmx
xcode6.3.1
squeezebox
hotswap
ivalueconverter
jchartfx
typesafe-stack
cloo
unrealscript
cmath
smartfox
clrprofiler
cakeyframeanimation
opends
algol68
airprint
solandra
resharper-5.0
yahoo-maps
downcasting
version-control-migration
isapi-redirect

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App