@@ -20,7 +20,7 @@ fn main() {
2020 test_nested_comprehension ( ) ;
2121 test_ownership_handling ( ) ;
2222 test_option ( ) ;
23- some_real_example_2 ( ) ;
23+ // some_real_example_2();
2424}
2525
2626fn test_vec ( ) {
@@ -412,13 +412,12 @@ fn some_real_example_1() {
412412 ) ;
413413}
414414
415+ #[ test]
415416fn some_real_example_2 ( ) {
416- #[ derive( Debug , PartialEq , Eq ) ]
417417 struct Score {
418418 subject : & ' static str ,
419419 score : u8 ,
420420 }
421- #[ derive( Debug , PartialEq , Eq ) ]
422421 struct Student {
423422 name : String ,
424423 age : u8 ,
@@ -456,113 +455,42 @@ fn some_real_example_2() {
456455 } ,
457456 ] ;
458457
459- let math_scores: HashMap < & String , u8 > = {
460- let mut math_scores = HashMap :: new ( ) ;
461- for student in & students_data {
462- for score in & student. scores {
463- if score. subject == "Math" {
464- math_scores. insert ( & student. name , score. score ) ;
465- }
466- }
467- }
468- math_scores
469- } ;
470- // ↓ 等价于 ↓
471- let math_scores: HashMap < & String , u8 > = hash_map ! [
472- & student. name => score. score
473- for student in & students_data
474- for score in & student. scores if score. subject == "Math"
475- ] ;
476-
477- assert_eq ! (
478- math_scores,
479- HashMap :: from( [ ( & "Alice" . to_string( ) , 95 ) , ( & "Bob" . to_string( ) , 78 ) ] )
480- ) ;
481-
458+ // 使用for loop
482459 let high_scores = {
483460 let mut high_scores = BTreeMap :: new ( ) ;
484- for student in & students_data {
461+ for Student { name , scores , .. } in & students_data {
485462 let mut subjects = Vec :: new ( ) ;
486- for score in & student . scores {
463+ for score in scores {
487464 if score. score >= 85 {
488465 subjects. push ( score. subject ) ;
489466 }
490467 }
491- high_scores. insert ( & student . name , subjects) ;
468+ high_scores. insert ( name, subjects) ;
492469 }
493470 high_scores
494471 } ;
495- // ↓ 等价于 ↓
496- let high_scores = b_tree_map ! [
497- & student. name =>
498- vector![ score. subject for score in & student. scores if score. score >= 85 ]
499- for student in & students_data
500- ] ;
501-
502- assert_eq ! (
503- high_scores,
504- BTreeMap :: from( [
505- ( & "Alice" . to_string( ) , vec![ "Math" , "English" ] ) ,
506- ( & "Bob" . to_string( ) , vec![ "English" ] )
507- ] )
508- ) ;
509- }
510-
511- #[ test]
512- fn some ( ) {
513- #[ derive( Debug , PartialEq , Eq ) ]
514- struct Score {
515- subject : & ' static str ,
516- score : u8 ,
517- }
518- #[ derive( Debug , PartialEq , Eq ) ]
519- struct Student {
520- name : String ,
521- age : u8 ,
522- scores : Vec < Score > ,
523- }
524-
525- let students_data = [
526- Student {
527- name : "Alice" . to_string ( ) ,
528- age : 20 ,
529- scores : vec ! [
530- Score {
531- subject: "Math" ,
532- score: 95 ,
533- } ,
534- Score {
535- subject: "English" ,
536- score: 88 ,
537- } ,
538- ] ,
539- } ,
540- Student {
541- name : "Bob" . to_string ( ) ,
542- age : 21 ,
543- scores : vec ! [
544- Score {
545- subject: "Math" ,
546- score: 78 ,
547- } ,
548- Score {
549- subject: "English" ,
550- score: 85 ,
551- } ,
552- ] ,
553- } ,
554- ] ;
555-
472+ // ↓ 等价于以下链式写法 ↓
473+ let high_scores = students_data
474+ . iter ( )
475+ . map ( |Student { name, scores, .. } | {
476+ (
477+ name,
478+ scores
479+ . iter ( )
480+ . filter ( |score| score. score >= 85 )
481+ . map ( |score| score. subject )
482+ . collect :: < Vec < _ > > ( ) ,
483+ )
484+ } )
485+ . collect :: < BTreeMap < _ , _ > > ( ) ;
486+ // ↓ 等价于以下推导式 ↓
556487 let high_scores = b_tree_map ! [
557- & student. name => high_scores
558- for student in & students_data
559- let high_scores = vector![
560- score. subject
561- for score in & student. scores if score. score >= 85
562- ]
563- let _ = {
564- println!( "{}" , high_scores. len( ) ) ;
565- }
488+ name => subjects
489+ for Student { name, scores, .. } in & students_data
490+ let subjects = vector![
491+ score. subject
492+ for score in scores. iter( ) if score. score >= 85
493+ ]
566494 ] ;
567495
568496 assert_eq ! (
0 commit comments