How to declare a variable but not assign it?

I want to declare acceptable_set and use it. But an empty vector is assigned to it. So the compiler warns. How to declare a variable and do not assign it? let mut acceptable_set: Vec<String> = Vec::new(); if opt.acceptable_set.is_none() { acceptable_set = crate::builtin_words::ACCEPTABLE .to_vec() .iter_mut() .map(|x| x.to_string()) .collect(); } else { acceptable_set = get_acceptable_set(opt) } warning:… Read More How to declare a variable but not assign it?

How to properly initialize a struct in Rust, with good enough encapsulations?

How to properly initialize a struct in Rust, with good enough encapsulations? Or more naively: how to leverage object/instance methods in the initialization/constructing process of structs? For example, as the initialization block in Kotlin: private class BinaryIndexedTree(nums: IntArray) { private val nNums = nums.size private val fenwick = IntArray(nNums + 1) { 0 } //… Read More How to properly initialize a struct in Rust, with good enough encapsulations?

Make difference between copy and direct initialization

Create an “UnusualClass” class in which direct and copy initialization produce different effects throughout. In particular, the attempt of direct or copy initialization should produce on the screen the print "Direct initialization" or "Copy initialization" #include <iostream> class UnusualClass{ public: UnusualClass(const int &n){ std::cout<<"Copy initialization"; } UnusualClass &operator=(const int &n){ std::cout<<"Direct initialization"; } }; int… Read More Make difference between copy and direct initialization

Why is it ok to assign a std::string& to a std::string variable in c++?

class MyClass: public: MyClass(const std::string& my_str); private: std::string _myStr In the implementation: MyClass::MyClass(const std::string& my_str): _mystr(my_str) How can you assign a reference (my_str is const std::string&) to a non-reference variable (_mystr, which is std::string) ? In my mental model, a variable is a block of space in memory can be filled with stuff that is… Read More Why is it ok to assign a std::string& to a std::string variable in c++?

argument type 'Future Function(int, String, String, String)' can't be assigned to the parameter type 'void Function(int, String?, String?, String?)?

import ‘package:flutter/foundation.dart’; import ‘package:flutter_local_notifications/flutter_local_notifications.dart’; class NotificationManager { // ignore: prefer_typing_uninitialized_variables var flutterLocalNotificationsPlugin; NotificationManager() { flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); initNotifications(); } getNotificationInstance() { return flutterLocalNotificationsPlugin; } void initNotifications() { // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project const initializationSettingsAndroid = AndroidInitializationSettings(‘@mipmap/launcher_icon’); const initializationSettingsIOS = IOSInitializationSettings( I… Read More argument type 'Future Function(int, String, String, String)' can't be assigned to the parameter type 'void Function(int, String?, String?, String?)?