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

Derive attribute for specific fields only, like serde does
How to access current cargo profile (build, test, bench, doc, …) from the build script (build.rs)
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> )

Categories

HOME
office365
functional-programming
windows-8.1
eclipse-plugin
devexpress
hdfs
out-of-memory
mongoid
autocomplete
tcp
swift2
iptables
linker
u-boot
native-base
tup
cocotb
port
elastic-load-balancer
migrate
sap-fiori
mpu6050
amazon-elb
squid
synthesis
reselect
google-cloud-logging
bazaar
libgit2
node-webkit
azure-machine-learning
hdl
bosun
multi-upload
acl
eclipse-emf
function-points
high-availability
webviewclient
amazon-iam
docx4j
caesar-cipher
gnu-classpath
pdfnet
djcelery
twilio-php
tidal-scheduler
fluid-mac-app-engine
web-inspector
transparency
moinmoin
react-intl
autorelease
windows-media-player
cloudwatch
winston
solr-query-syntax
sequence-diagram
gtk#
google-news
connect-direct
polymaps
annotatorjs
haraka
ndk-build
livereload
seccomp
msbuild-4.0
static-methods
mathnet
as3-api
wepay
servicestack-bsd
place
srv-record
uitest
rubber
ildasm
igor
angular-ui-select
coding-efficiency
xmi
jchartfx
lync-2010
san
django-facebook
postgres-xc
jbox2d
chefspec
data-dump
3des
downgrade
resharper-5.1
aggregator
rational-unified-process
firephp
watchpoint
regioninfo
word-automation

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