# ojeriejire rust version in python def split(string, separator): split_list = [] i = 0 for j, c in enumerate(string): if c == separator: slice_ = string[i:j] split_list.append(slice_) i = j + 1 split_list.append(string[i:]) return split_list
fn split(string: &str, separator: char) -> Vec<&str> {let mut split = Vec::new();let mut i = 0;- # ojeriejire rust version in python
- def split(string, separator):
- split_list = []
- i = 0
for (j, c) in string.char_indices() {if c == separator {let slice = &string[i..j];split.push(slice);i = j + 1;}}- for j, c in enumerate(string):
- if c == separator:
- slice_ = string[i:j]
- split_list.append(slice_)
- i = j + 1
split.push(&string[i..]);return split;}- split_list.append(string[i:])
- return split_list
#[test] def test(): assert split("Hello World", ' ') == ["Hello", "World"] assert split("John-brings-his-cat", '-') == ["John", "brings", "his", "cat"] assert split(",joe,mama,", ',') == ["", "joe", "mama", ""] print("All test cases passed.") # Running the test function to check if the function works test()
- #[test]
fn test() {assert_eq!(split("Hello World", ' '), ["Hello", "World"]);assert_eq!(split("John-brings-his-cat", '-'), ["John", "brings", "his", "cat"]);assert_eq!(split(",joe,mama,", ','), ["", "joe", "mama", ""]);}- def test():
- assert split("Hello World", ' ') == ["Hello", "World"]
- assert split("John-brings-his-cat", '-') == ["John", "brings", "his", "cat"]
- assert split(",joe,mama,", ',') == ["", "joe", "mama", ""]
- print("All test cases passed.")
- # Running the test function to check if the function works
- test()