精華區beta Marginalman 關於我們 聯絡資訊
※ 引述《ZooseWu (動物園 公告)》之銘言: : 1662. Check If Two String Arrays are Equivalent : 給你兩個字串陣列 : 回傳兩個陣列是否表示相同字串 : 把陣列元素按順序連接在一起之後就是他的字串 : Approach: : 用join合併之後直接比較 Rust可以直接用concat解: impl Solution { pub fn array_strings_are_equal(word1: Vec<String>, word2: Vec<String>) -> bool { word1.concat() == word2.concat() } } : 推 smart0eddie: 這是不是想考字串比較的function 實作 12/01 10:20 : → ZooseWu: 不清楚 他難度只有easy 12/01 10:31 Rust有一種叫做fold的方法可以串接東西: impl Solution { pub fn array_strings_are_equal(word1: Vec<String>, word2: Vec<String>) -> bool { let concat1 = word1.iter().fold(String::new(), |mut acc, word| { acc.push_str(word); acc }); let concat2 = word2.iter().fold(String::new(), |mut acc, word| { acc.push_str(word); acc }); concat1 == concat2 } } fold的閉包輸入有一個acc累加器跟一個value 總之透過條件篩選就能用fold取得一個容器裡面需要的東西 像是取偶數和可以這樣: pub fn main() { let even_sum = (1..=10).fold(0, |acc, num| if num % 2 == 0 { acc + num } else { acc }); println!("{even_sum:?}"); } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.143.172 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1701398645.A.196.html
wwndbk: 大師 12/01 10:50
ZooseWu: rust是不是也是一個對FP支援程度很高的語言阿 12/01 10:51
yam276: 應該吧 這語言用很前衛的皮套很低層的東西 12/01 10:51
yam276: 像光是match就能玩一堆東西 12/01 10:51
Rust: Rust好強 12/02 20:22