Rust pattern matching with multiple types -


is possible pattern match in rust multiple types? seems though can't. if not, there planned support add or run-time type information (rtti)?

struct bus; struct car; struct person;  fn main() {     let x = bus;     //or more realistically, let x = function_with_multiple_return_types();      match x {         car => {             // ...         }         bus => {             // gets executed         }         person => {             // ...         }     } } 

this example trivial. in real life, useful if x multiple types. e.g. let x = function_with_multiple_return_types();. shepmaster says, not possible because rust statically typed, unlike lisp.

no 1 can 100% accuracy feature or won't ever implemented, can 100% belief never implemented.

why that? because there's 0 benefit proposed syntax. rust statically-typed language. means compiler knows type of variable is. there's no way that branch besides bus ever executed. there's no way variable can have more 1 type! why language change allow add code never used? wouldn't useful.

a match statement, , pattern matching in general, useful when there multiple possible variants of something. that's why rust has enums; allow fixed set of dynamic choices (a.k.a. made @ runtime).

if need open set of dynamic decisions, that's traits (and maybe specialization) for. there's trait allows any concrete type.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -