Levenshtein distance: Difference between revisions

→‎{{header|Rust}}: more concise and efficient initialization of 'matrix'
(Added Wren)
(→‎{{header|Rust}}: more concise and efficient initialization of 'matrix')
Line 4,763:
=={{header|Rust}}==
Implementation of the wikipedia algorithm.
{{works with|Rust|1.145}}
<lang rust>fn main() {
println!("{}", levenshtein_distance("kitten", "sitting"));
Line 4,769:
println!("{}", levenshtein_distance("rosettacode", "raisethysword"));
}
 
fn levenshtein_distance(word1: &str, word2: &str) -> usize {
let w1 = word1.chars().collect::<Vec<_>>();
let w2 = word2.chars().collect::<Vec<_>>();
 
let word1_length = w1.len() + 1;
let word2_length = w2.len() + 1;
 
let mut matrix = vec![vec![0; word1_length]; word2_length];
 
for i in 1..word1_length { matrix[0].push([i] = i); }
for j in 1..word2_length { matrix.push(vec![j])[0] = j; }
 
for j in 1..word2_length {
for i in 1..word1_length {
Line 4,791:
, matrix[j-1][i-1])
};
matrix[j].push([i] = x);
}
}
Anonymous user