Reputation: 121
I want a function that can take two arguments (string, number of letters to crop off front)
and return the same string except with the letters before character x gone.
If I write
let mut example = "stringofletters";
CropLetters(example, 3);
println!("{}", example);
then the output should be:
ingofletters
Is there any way I can do this?
Upvotes: 7
Views: 7159
Reputation: 141
my version
fn crop_str(s: &str, n: usize) -> &str {
let mut it = s.chars();
for _ in 0..n {
it.next();
}
it.as_str()
}
#[test]
fn test_crop_str() {
assert_eq!(crop_str("123", 1), "23");
assert_eq!(crop_str("ЖФ1", 1), "Ф1");
assert_eq!(crop_str("ЖФ1", 2), "1");
}
Upvotes: 0
Reputation: 14051
In many uses it would make sense to simply return a slice of the input, avoiding any copy. Converting @Shepmaster's solution to use immutable slices:
fn crop_letters(s: &str, pos: usize) -> &str {
match s.char_indices().skip(pos).next() {
Some((pos, _)) => &s[pos..],
None => "",
}
}
fn main() {
let example = "stringofletters"; // works with a String if you take a reference
let cropped = crop_letters(example, 3);
println!("{}", cropped);
}
Advantages over the mutating version are:
cropped.to_string()
if you want a newly allocated result; but you don't have to.String
etc.The disadvantage is that if you really do have a mutable string you want to modify, it would be slightly less efficient as you'd need to allocate a new String
.
Upvotes: 12
Reputation: 430921
Issues with your original code:
snake_case
, types and traits use CamelCase
."foo"
is a string literal of type &str
. These may not be changed. You will need something that has been heap-allocated, such as a String
.crop_letters(stringofletters, 3)
would transfer ownership of stringofletters
to the method, which means you wouldn't be able to use the variable anymore. You must pass in a mutable reference (&mut
).char_indices
is a good tool here.drain
to move a chunk of bytes out of the string. We just drop these bytes and let the String
move over the remaining bytes. fn crop_letters(s: &mut String, pos: usize) {
match s.char_indices().nth(pos) {
Some((pos, _)) => {
s.drain(..pos);
}
None => {
s.clear();
}
}
}
fn main() {
let mut example = String::from("stringofletters");
crop_letters(&mut example, 3);
assert_eq!("ingofletters", example);
}
See Chris Emerson's answer if you don't actually need to modify the original String
.
Upvotes: 7
Reputation: 163
I found this answer which I don't consider really idiomatic:
fn crop_with_allocation(string: &str, len: usize) -> String {
string.chars().skip(len).collect()
}
fn crop_without_allocation(string: &str, len: usize) -> &str {
// optional length check
if string.len() < len {
return &"";
}
&string[len..]
}
fn main() {
let example = "stringofletters"; // works with a String if you take a reference
let cropped = crop_with_allocation(example, 3);
println!("{}", cropped);
let cropped = crop_without_allocation(example, 3);
println!("{}", cropped);
}
Upvotes: 0