खोज…


वाक्य - विन्यास

  • _ // वाइल्डकार्ड पैटर्न, कुछ भी मेल खाता है
  • पहचान // बाध्यकारी पैटर्न, किसी भी चीज से मेल खाता है और पहचान के लिए इसे बांधता है matches
  • @ pat // समान ऊपर के रूप में पहचाना जाता है, लेकिन आगे क्या मिलान किया जाता है इसकी अनुमति दें
  • रेफरी अध्यक्ष // बाध्यकारी पैटर्न, मैचों कुछ भी और यह बांधता है संदर्भ अध्यक्ष को ¹
  • रेफरी आइडेंटिफिकेशन // बाइंडिंग पैटर्न, किसी भी चीज से मेल खाता है और इसे एक म्यूट रेफरेंस आइडेंट pattern से बांधता है
  • & pat // एक संदर्भ से मेल खाता है ( pat इसलिए संदर्भ नहीं बल्कि रेफरी है) reference
  • & mut pat // उसी के रूप में ऊपर एक उत्परिवर्तनीय संदर्भ के साथ
  • CONST // एक नामित स्थिरांक से मेल खाता है
  • Struct {फ़ील्ड 1, field2} // मैच और एक संरचना मूल्य deconstructs, fields¹ के बारे में टिप्पणी नीचे देखें
  • EnumVariant // एक गणना संस्करण से मेल खाता है
  • EnumVariant ( pat1 , pat2 ) // एक एन्यूमरेशन वेरिएंट और संबंधित मापदंडों से मेल खाता है
  • EnumVariant ( pat1 , pat2 , .., patn ) // ऊपर के समान ही लेकिन सभी पहले, दूसरे और अंतिम मापदंडों को छोड़ देता है
  • ( pat1 , pat2 ) // एक ट्यूपल और संबंधित एलिमेंट से मेल खाता है
  • ( pat1 , pat2 , .., patn ) // ऊपर जैसा ही है, लेकिन सभी पहले, दूसरे और अंतिम तत्वों को छोड़ देता है
  • जलाया // एक शाब्दिक स्थिरांक (चार, संख्यात्मक प्रकार, बूलियन और स्ट्रिंग) से मेल खाता है
  • pat1 ... pat2 // उस (समावेशी) श्रेणी (चार और संख्यात्मक प्रकार) में एक मूल्य से मेल खाता है

टिप्पणियों

जब एक संरचना मूल्य को डिकंस्ट्रक्ट करते हैं, तो फ़ील्ड या तो फॉर्म field_name या field_name : pattern होना चाहिए। यदि कोई पैटर्न निर्दिष्ट नहीं है, तो एक अंतर्निहित बाध्यकारी किया जाता है:

let Point { x, y } = p;
// equivalent to
let Point { x: x, y: y } = p;

let Point { ref x, ref y } = p;
// equivalent to
let Point { x: ref x, y: ref y } = p;

1: अकाट्य पैटर्न

बाइंडिंग के साथ पैटर्न मिलान

मानों को @ का उपयोग करके नामों से बाँधना संभव है:


struct Badger {
    pub age: u8
}

fn main() {
    // Let's create a Badger instances
    let badger_john = Badger { age: 8 };

    // Now try to find out what John's favourite activity is, based on his age
    match badger_john.age {
        // we can bind value ranges to variables and use them in the matched branches
        baby_age @ 0...1 => println!("John is {} years old, he sleeps a lot", baby_age),
        young_age @ 2...4 => println!("John is {} years old, he plays all day", young_age),
        adult_age @ 5...10 => println!("John is {} years old, he eats honey most of the time", adult_age),
        old_age => println!("John is {} years old, he mostly reads newspapers", old_age),
    }
}

यह प्रिंट करेगा:

John is 8 years old, he eats honey most of the time

बुनियादी पैटर्न मिलान

// Create a boolean value
let a = true;

// The following expression will try and find a pattern for our value starting with
// the topmost pattern. 
// This is an exhaustive match expression because it checks for every possible value
match a {
  true => println!("a is true"),
  false => println!("a is false")
}

यदि हम हर मामले को कवर नहीं करते हैं तो हमें एक कंपाइलर त्रुटि मिलेगी:

match a {
  true => println!("most important case")
}
// error: non-exhaustive patterns: `false` not covered [E0004]

हम _ उपयोग डिफ़ॉल्ट / वाइल्डकार्ड केस के रूप में कर सकते हैं, यह सब कुछ से मेल खाता है:

// Create an 32-bit unsigned integer
let b: u32 = 13;

match b {
  0 => println!("b is 0"),
  1 => println!("b is 1"),
  _ => println!("b is something other than 0 or 1")
}

यह उदाहरण छपेगा:

a is true
b is something else than 0 or 1

कई पैटर्न का मिलान

यह इलाज कई संभव है, अलग-अलग मान उसी तरह, का उपयोग कर | :

enum Colour {
    Red,
    Green,
    Blue,
    Cyan,
    Magenta,
    Yellow,
    Black
}

enum ColourModel {
    RGB,
    CMYK
}

// let's take an example colour
let colour = Colour::Red;

let model = match colour {
    // check if colour is any of the RGB colours
    Colour::Red | Colour::Green | Colour::Blue => ColourModel::RGB,
    // otherwise select CMYK
    _ => ColourModel::CMYK,
};

सशर्त पैटर्न गार्ड के साथ मेल खाते हैं

if गार्ड का उपयोग करके मिलान किए जा रहे मूल्य के आधार पर पैटर्न का मिलान मूल्यों के आधार पर किया जा सकता है:

// Let's imagine a simplistic web app with the following pages:
enum Page {
  Login,
  Logout,
  About,
  Admin
}

// We are authenticated
let is_authenticated = true;

// But we aren't admins
let is_admin = false;

let accessed_page = Page::Admin;

match accessed_page {
    // Login is available for not yet authenticated users
    Page::Login if !is_authenticated => println!("Please provide a username and a password"),

    // Logout is available for authenticated users 
    Page::Logout if is_authenticated => println!("Good bye"),
    
    // About is a public page, anyone can access it
    Page::About => println!("About us"),

    // But the Admin page is restricted to administators
    Page::Admin if is_admin => println!("Welcome, dear administrator"),

    // For every other request, we display an error message
    _ => println!("Not available")
}

यह "उपलब्ध नहीं" प्रदर्शित करेगा।

अगर / जाने दो


if let

एक पैटर्न match और एक if स्टेटमेंट को मिलाता है, और संक्षिप्त गैर-थकाऊ मैचों के लिए अनुमति देता है।

if let Some(x) = option {
    do_something(x);
}

यह इसके बराबर है:

match option {
    Some(x) => do_something(x),
    _ => {},
}

इन ब्लॉकों में else भी बयान हो सकते हैं।

if let Some(x) = option {
    do_something(x);
} else {
    panic!("option was None");
}

यह ब्लॉक इसके बराबर है:

match option {
    Some(x) => do_something(x),
    None => panic!("option was None"),
}

while let

एक पैटर्न मैच और थोड़ी देर लूप को जोड़ती है।

let mut cs = "Hello, world!".chars();
while let Some(x) = cs.next() {
    print("{}+", x);
}
println!("");

यह प्रिंट H+e+l+l+o+,+ +w+o+r+l+d+!+

यह loop {} और match स्टेटमेंट का उपयोग करने के बराबर है:

let mut cs = "Hello, world!".chars();
loop {
    match cs.next() {
        Some(x) => print("{}+", x),
        _ => break,
    }
}
println!("");

पैटर्न से संदर्भ निकालना

कभी-कभी केवल संदर्भ (यानी स्वामित्व को स्थानांतरित किए बिना) का उपयोग करके किसी ऑब्जेक्ट से मान निकालने में सक्षम होना आवश्यक है।

struct Token {
  pub id: u32
}

struct User {
  pub token: Option<Token>
}


fn main() {
    // Create a user with an arbitrary token
    let user = User { token: Some(Token { id: 3 }) };

    // Let's borrow user by getting a reference to it
    let user_ref = &user;

    // This match expression would not compile saying "cannot move out of borrowed
    // content" because user_ref is a borrowed value but token expects an owned value.
    match user_ref {
        &User { token } => println!("User token exists? {}", token.is_some())
    }

    // By adding 'ref' to our pattern we instruct the compiler to give us a reference
    // instead of an owned value.
    match user_ref {
        &User { ref token } => println!("User token exists? {}", token.is_some())
    }

    // We can also combine ref with destructuring
    match user_ref {
        // 'ref' will allow us to access the token inside of the Option by reference
        &User { token: Some(ref user_token) } => println!("Token value: {}", user_token.id ),
        &User { token: None } => println!("There was no token assigned to the user" )
    }

    // References can be mutable too, let's create another user to demonstrate this
    let mut other_user = User { token: Some(Token { id: 4 }) };

    // Take a mutable reference to the user
    let other_user_ref_mut = &mut other_user;

    match other_user_ref_mut {
        // 'ref mut' gets us a mutable reference allowing us to change the contained value directly.
        &mut User { token: Some(ref mut user_token) } => {
            user_token.id = 5;
            println!("New token value: {}", user_token.id )
        },
        &mut User { token: None } => println!("There was no token assigned to the user" )
    }
}

यह इसे प्रिंट करेगा:

User token exists? true
Token value: 3
New token value: 5


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow