rust


Using Rust, is there a faster/shorter way to initialize variables in a struct?


In the following example, I would much prefer to assign a value to each field in the struct in the declaration of the fields. Alternatively, it effectively takes one additional statement for each field to assign a value to the fields. All I want to be able to do is to assign default values when the struct is instantiated.
Is there a less-verbose way of doing this?
struct cParams {
iInsertMax : i64,
iUpdateMax : i64,
iDeleteMax : i64,
iInstanceMax : i64,
tFirstInstance : bool,
tCreateTables : bool,
tContinue : bool
}
impl cParams {
fn new() -> cParams {
cParams {iInsertMax : -1, iUpdateMax : -1, iDeleteMax : -1, iInstanceMax : -1,
tFirstInstance : false, tCreateTables : false, tContinue : false}
}
You can provide default values for your struct by implementing the Default trait. The default function would look like your current new function:
impl Default for cParams {
fn default () -> cParams {
cParams {iInsertMax : -1, iUpdateMax : -1, iDeleteMax : -1, iInstanceMax : -1, tFirstInstance : false, tCreateTables : false, tContinue : false}
}
}
You can then instantiate the struct by giving only the non-default values:
let p = cParams { iInsertMax: 10, ..Default::default() };
With some minor changes to your data structure, you can take advantage of an automatically derived default implementation. If you use #[derive(Default)] on a data structure, the compiler will automatically create a default function for you that fills each field with its default value. The default boolean value is false, the default int value is 0.
An integer's default value being 0 is a problem here since you want the integer fields to be -1 by default. You could define a new type that implements a default value of -1 and use that instead of i64 in your struct. (I haven't tested that, but it should work).
However, I'd suggest to slightly change your data structure and use Option<i64> instead of i64. I don't know the context of your code, but it looks like you're using the special value of -1 to represent the special meaning "infinite", or "there's no max". In Rust, we use an Option to represent an optionally present value. There's no need for a -1 hack. An option can be either None or Some(x) where x would be your i64 here. It might even be an unsigned integer if -1 was the only negative value. Btw, the default option value is None, so with the proposed changes, your code could look like this:
#[derive(Default)]
struct cParams {
iInsertMax: Option<u64>,
iUpdateMax: Option<u64>,
iDeleteMax: Option<u64>,
iInstanceMax: Option<u64>,
tFirstInstance: bool,
tCreateTables: bool,
tContinue: bool,
}
let p = cParams { iInsertMax: Some(10), ..Default::default() };

Related Links

How to create a non consuming iterator from a Vector
How to mutate struct field in method
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

Categories

HOME
hpoo
tomcat
shinyapps
compilation
consul
google-play-services
header
dependencies
ui-automation
port
gtk
hystrix
siesta
graphdb
fullcalendar-scheduler
ex
texas-instruments
cocos2d-android
word2vec
coroutine
android-custom-view
hdl
substring
sybase-ase
environment
pubxml
tf-idf
jackrabbit-oak
gravity-forms-plugin
react-bootstrap-table
miniprofiler
substance
guice
toad
windows2012
lxml
microsoft-certifications
listjs
advanced-filter
spring-rabbitmq
dreamfactory
pdfminer
jslint
widestring
klee
oauth2-playground
minikube
virtual-memory
viewmodel
tinyioc
roracle
image-registration
libreoffice-writer
supertest
visual-format-language
patternlab.io
test-data
emacs25
mechanicalturk
gawk
s-function
openquery
nvda
declarative
piping
ajaxmin
management-studio-express
ioexception
m4
android-mapview
mousehover
openshift-cartridge
featuretoggle
wso2ml
espresso
supercomputers
darwin
angular-amd
gflags
inequality
application-server
mp4parser
disjoint-union
lru
.net-remoting
microformats
blobstorage
reentrancy
jquery-cycle
ckeditor.net
word-frequency
telerik-ajax
java.lang.class
affinetransform
net-use
firefly-mv
upshot
asp.net-mvc-views
airprint
pyjamas
active-record-query
tessellation
aspbutton
mysqli-multi-query
motif
suggestbox
rootkit

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