@@ -18,20 +18,16 @@ fn main() {
1818}
1919
2020fn app ( cx : Scope ) -> Element {
21- let ( display_value , set_display_value ) = use_state ( & cx, || String :: from ( "0" ) ) ;
21+ let val = use_state ( & cx, || String :: from ( "0" ) ) ;
2222
2323 let input_digit = move |num : u8 | {
24- if display_value == "0" {
25- set_display_value ( String :: new ( ) ) ;
24+ if val . get ( ) == "0" {
25+ val . set ( String :: new ( ) ) ;
2626 }
27- set_display_value
28- . make_mut ( )
29- . push_str ( num. to_string ( ) . as_str ( ) ) ;
27+ val. make_mut ( ) . push_str ( num. to_string ( ) . as_str ( ) ) ;
3028 } ;
3129
32- let input_operator = move |key : & str | {
33- set_display_value. make_mut ( ) . push_str ( key) ;
34- } ;
30+ let input_operator = move |key : & str | val. make_mut ( ) . push_str ( key) ;
3531
3632 cx. render ( rsx ! (
3733 style { [ include_str!( "./assets/calculator.css" ) ] }
@@ -54,43 +50,43 @@ fn app(cx: Scope) -> Element {
5450 KeyCode :: Num8 => input_digit( 8 ) ,
5551 KeyCode :: Num9 => input_digit( 9 ) ,
5652 KeyCode :: Backspace => {
57- if !display_value . len( ) != 0 {
58- set_display_value . make_mut( ) . pop( ) ;
53+ if !val . len( ) != 0 {
54+ val . make_mut( ) . pop( ) ;
5955 }
6056 }
6157 _ => { }
6258 } ,
63- div { class: "calculator-display" , [ display_value . to_string( ) ] }
59+ div { class: "calculator-display" , [ val . to_string( ) ] }
6460 div { class: "calculator-keypad" ,
6561 div { class: "input-keys" ,
6662 div { class: "function-keys" ,
6763 button {
6864 class: "calculator-key key-clear" ,
6965 onclick: move |_| {
70- set_display_value ( String :: new( ) ) ;
71- if !display_value . is_empty( ) {
72- set_display_value ( "0" . into( ) ) ;
66+ val . set ( String :: new( ) ) ;
67+ if !val . is_empty( ) {
68+ val . set ( "0" . into( ) ) ;
7369 }
7470 } ,
75- [ if display_value . is_empty( ) { "C" } else { "AC" } ]
71+ [ if val . is_empty( ) { "C" } else { "AC" } ]
7672 }
7773 button {
7874 class: "calculator-key key-sign" ,
7975 onclick: move |_| {
80- let temp = calc_val( display_value . clone ( ) ) ;
76+ let temp = calc_val( val . as_str ( ) ) ;
8177 if temp > 0.0 {
82- set_display_value ( format!( "-{}" , temp) ) ;
78+ val . set ( format!( "-{}" , temp) ) ;
8379 } else {
84- set_display_value ( format!( "{}" , temp. abs( ) ) ) ;
80+ val . set ( format!( "{}" , temp. abs( ) ) ) ;
8581 }
8682 } ,
8783 "±"
8884 }
8985 button {
9086 class: "calculator-key key-percent" ,
9187 onclick: move |_| {
92- set_display_value (
93- format!( "{}" , calc_val( display_value . clone ( ) ) / 100.0 )
88+ val . set (
89+ format!( "{}" , calc_val( val . as_str ( ) ) / 100.0 )
9490 ) ;
9591 } ,
9692 "%"
@@ -100,7 +96,7 @@ fn app(cx: Scope) -> Element {
10096 button { class: "calculator-key key-0" , onclick: move |_| input_digit( 0 ) ,
10197 "0"
10298 }
103- button { class: "calculator-key key-dot" , onclick: move |_| set_display_value . make_mut( ) . push( '.' ) ,
99+ button { class: "calculator-key key-dot" , onclick: move |_| val . make_mut( ) . push( '.' ) ,
104100 "●"
105101 }
106102 ( 1 ..10 ) . map( |k| rsx!{
@@ -114,25 +110,21 @@ fn app(cx: Scope) -> Element {
114110 }
115111 }
116112 div { class: "operator-keys" ,
117- button { class: "calculator-key key-divide" ,
118- onclick: move |_| input_operator( "/" ) ,
113+ button { class: "calculator-key key-divide" , onclick: move |_| input_operator( "/" ) ,
119114 "÷"
120115 }
121- button { class: "calculator-key key-multiply" ,
122- onclick: move |_| input_operator( "*" ) ,
116+ button { class: "calculator-key key-multiply" , onclick: move |_| input_operator( "*" ) ,
123117 "×"
124118 }
125- button { class: "calculator-key key-subtract" ,
126- onclick: move |_| input_operator( "-" ) ,
119+ button { class: "calculator-key key-subtract" , onclick: move |_| input_operator( "-" ) ,
127120 "−"
128121 }
129- button { class: "calculator-key key-add" ,
130- onclick: move |_| input_operator( "+" ) ,
122+ button { class: "calculator-key key-add" , onclick: move |_| input_operator( "+" ) ,
131123 "+"
132124 }
133125 button { class: "calculator-key key-equals" ,
134126 onclick: move |_| {
135- set_display_value ( format!( "{}" , calc_val( display_value . clone ( ) ) ) ) ;
127+ val . set ( format!( "{}" , calc_val( val . as_str ( ) ) ) ) ;
136128 } ,
137129 "="
138130 }
@@ -145,7 +137,7 @@ fn app(cx: Scope) -> Element {
145137 ) )
146138}
147139
148- fn calc_val ( val : String ) -> f64 {
140+ fn calc_val ( val : & str ) -> f64 {
149141 let mut temp = String :: new ( ) ;
150142 let mut operation = "+" . to_string ( ) ;
151143
0 commit comments