RANDOM_RUST_DEV Telegram 208
Rust Tip дня: матчинг слайсов.

В Rust можно элегантно извлекать элементы из слайсов с помощью паттерн-матчинга:

match input {
[0xFF, rest @ ..] => println!("Starts with 0xFF, then {:?}", rest),
[first, second, .., last] => println!("Start: {first:#X}, {second:#X}, end: {last:#X}"),
[.., last] => println!("Ends with {last:#X}"),
[] => println!("Empty slice"),
}


Извлечение с одним вариантом.
if let [a, b, c] = input {
println!("Exactly three elements: {a}, {b}, {c}");
}


Извлечение с ранним возвратом при ошибке матчинга.

let [first, second, rest @ ..] = input else {
panic!("Expected at least two elements");
};
println!("Parsed: {first}, {second}, rest = {:?}", rest);


Сила pattern matching раскрывается в полной мере, когда вы:

* избегаете ручного индексирования,
* делаете распаковку данных декларативной,
* явно документируете формат входа через структуру паттерна.


Приятного вам матчинга.



tgoop.com/random_rust_dev/208
Create:
Last Update:

Rust Tip дня: матчинг слайсов.

В Rust можно элегантно извлекать элементы из слайсов с помощью паттерн-матчинга:

match input {
[0xFF, rest @ ..] => println!("Starts with 0xFF, then {:?}", rest),
[first, second, .., last] => println!("Start: {first:#X}, {second:#X}, end: {last:#X}"),
[.., last] => println!("Ends with {last:#X}"),
[] => println!("Empty slice"),
}


Извлечение с одним вариантом.
if let [a, b, c] = input {
println!("Exactly three elements: {a}, {b}, {c}");
}


Извлечение с ранним возвратом при ошибке матчинга.

let [first, second, rest @ ..] = input else {
panic!("Expected at least two elements");
};
println!("Parsed: {first}, {second}, rest = {:?}", rest);


Сила pattern matching раскрывается в полной мере, когда вы:

* избегаете ручного индексирования,
* делаете распаковку данных декларативной,
* явно документируете формат входа через структуру паттерна.


Приятного вам матчинга.

BY Random Rust Dev


Share with your friend now:
tgoop.com/random_rust_dev/208

View MORE
Open in Telegram


Telegram News

Date: |

To delete a channel with over 1,000 subscribers, you need to contact user support Hui said the time period and nature of some offences “overlapped” and thus their prison terms could be served concurrently. The judge ordered Ng to be jailed for a total of six years and six months. The initiatives announced by Perekopsky include monitoring the content in groups. According to the executive, posts identified as lacking context or as containing false information will be flagged as a potential source of disinformation. The content is then forwarded to Telegram's fact-checking channels for analysis and subsequent publication of verified information. Judge Hui described Ng as inciting others to “commit a massacre” with three posts teaching people to make “toxic chlorine gas bombs,” target police stations, police quarters and the city’s metro stations. This offence was “rather serious,” the court said. How to build a private or public channel on Telegram?
from us


Telegram Random Rust Dev
FROM American