You could convert the Vec
into an iterator, and write this in fluent style:.
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v
.into_iter()
.skip(2)
.collect();
println!("{}", res); // "cd"
}
Instead of skip(2)
on the iterator, you could convert a slice of the Vec
:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v[2..]
.into_iter()
.collect();
println!("{}", res); // "cd"
}
I find this very readable, and it generates fast code. One advantage it has is that res
no longer needs to be mut
, but can be a static single assignment. To achieve that with the loop implementation, you would need to move the loop into a helper, or "phi," function.
In real-world code, a container like v
, whose data are compile-time constants and never modified, would be a const
or static
array, not a Vec
.
Recall that a Rust String
holds UTF-8 bytes, not an array of UCS-4 char
. There’s a function to move a Vec<u8>
that holds valid UTF-8 bytes into a String
: String::from_utf8
. It’s likely that your input will be in UTF-8 as well, so if you can avoid the overhead of converting between UCS-4 and UTF-8 or vice versa, that will be far more efficient.
You could convert the Vec
into an iterator, and write this in fluent style:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v
.into_iter()
.skip(2)
.collect();
println!("{}", res); // "cd"
}
Instead of skip(2)
on the iterator, you could convert a slice of the Vec
:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v[2..]
.into_iter()
.collect();
println!("{}", res); // "cd"
}
In real-world code, a container like v
, whose data are compile-time constants and never modified, would be a const
or static
array, not a Vec
.
Recall that a Rust String
holds UTF-8 bytes, not an array of UCS-4 char
. There’s a function to move a Vec<u8>
that holds valid UTF-8 bytes into a String
: String::from_utf8
. It’s likely that your input will be in UTF-8 as well, so if you can avoid the overhead of converting between UCS-4 and UTF-8 or vice versa, that will be far more efficient.
You could convert the Vec
into an iterator, and write this in fluent style.
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v
.into_iter()
.skip(2)
.collect();
println!("{}", res); // "cd"
}
Instead of skip(2)
on the iterator, you could convert a slice of the Vec
:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v[2..]
.into_iter()
.collect();
println!("{}", res); // "cd"
}
I find this very readable, and it generates fast code. One advantage it has is that res
no longer needs to be mut
, but can be a static single assignment. To achieve that with the loop implementation, you would need to move the loop into a helper, or "phi," function.
In real-world code, a container like v
, whose data are compile-time constants and never modified, would be a const
or static
array, not a Vec
.
Recall that a Rust String
holds UTF-8 bytes, not an array of UCS-4 char
. There’s a function to move a Vec<u8>
that holds valid UTF-8 bytes into a String
: String::from_utf8
. It’s likely that your input will be in UTF-8 as well, so if you can avoid the overhead of converting between UCS-4 and UTF-8 or vice versa, that will be far more efficient.
You could convert the Vec
into an iterator, and write this in fluent style:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v
.into_iter()
.skip(2)
.collect();
println!("{}", res); // "cd"
}
Instead of skip(2)
on the iterator, you could convert a slice of the Vec
:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v[2..]
.into_iter()
.collect();
println!("{}", res); // "cd"
}
In real-world code, a container like v
, whose data are compile-time constants and never modified, would be a const
or static
array, not a Vec
.
Recall that a Rust String
holds UTF-8 bytes, not an array of UCS-4 char
. There’s a function to move a Vec<u8>
that holds valid UTF-8 bytes into a String
: String::from_utf8
. It’s likely that your input will be in UTF-8 as well, so if you can avoid the overhead of converting between UCS-4 and UTF-8 or vice versa, that will be far more efficient.
You could convert the Vec
into an iterator, and write this in fluent style:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v
.into_iter()
.skip(2)
.collect();
println!("{}", res); // "cd"
}
Instead of skip(2)
on the iterator, you could convert a slice of the Vec
:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v[2..]
.into_iter()
.collect();
println!("{}", res); // "cd"
}
Recall that a Rust String
holds UTF-8 bytes, not an array of UCS-4 char
. There’s a function to move a Vec<u8>
that holds valid UTF-8 bytes into a String
: String::from_utf8
. It’s likely that your input will be in UTF-8 as well, so if you can avoid the overhead of converting between UCS-4 and UTF-8 or vice versa, that will be far more efficient.
You could convert the Vec
into an iterator, and write this in fluent style:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v
.into_iter()
.skip(2)
.collect();
println!("{}", res); // "cd"
}
Instead of skip(2)
on the iterator, you could convert a slice of the Vec
:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v[2..]
.into_iter()
.collect();
println!("{}", res); // "cd"
}
In real-world code, a container like v
, whose data are compile-time constants and never modified, would be a const
or static
array, not a Vec
.
Recall that a Rust String
holds UTF-8 bytes, not an array of UCS-4 char
. There’s a function to move a Vec<u8>
that holds valid UTF-8 bytes into a String
: String::from_utf8
. It’s likely that your input will be in UTF-8 as well, so if you can avoid the overhead of converting between UCS-4 and UTF-8 or vice versa, that will be far more efficient.
You could convert the Vec
into an iterator, and write this in fluent style:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v
.into_iter()
.skip(2)
.collect();
println!("{}", res); // "cd"
}
Instead of skip(2)
on the iterator, you could convert a slice of the Vec
:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v[2..]
.into_iter()
.collect();
println!("{}", res); // "cd"
}
If theRecall that a Rust Vec<u8>String
holds UTF-8 bytes, there’snot an array of UCS-4 char
. There’s a function to move ita Vec<u8>
that holds valid UTF-8 bytes into a String
: String::from_utf8
. It’s likely that your input will be in UTF-8 as well, so if you can avoid the overhead of converting between UCS-4 and UTF-8 or vice versa, that will be far more efficient.
You could convert the Vec
into an iterator, and write this in fluent style:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v
.into_iter()
.skip(2)
.collect();
println!("{}", res); // "cd"
}
Instead of skip(2)
on the iterator, you could convert a slice of the Vec
:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v[2..]
.into_iter()
.collect();
println!("{}", res); // "cd"
}
If the Vec<u8>
holds UTF-8 bytes, there’s a function to move it into a String
: String::from_utf8
.
You could convert the Vec
into an iterator, and write this in fluent style:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v
.into_iter()
.skip(2)
.collect();
println!("{}", res); // "cd"
}
Instead of skip(2)
on the iterator, you could convert a slice of the Vec
:
fn main() {
let v = vec!['a', 'b', 'c', 'd'];
let res : String = v[2..]
.into_iter()
.collect();
println!("{}", res); // "cd"
}
Recall that a Rust String
holds UTF-8 bytes, not an array of UCS-4 char
. There’s a function to move a Vec<u8>
that holds valid UTF-8 bytes into a String
: String::from_utf8
. It’s likely that your input will be in UTF-8 as well, so if you can avoid the overhead of converting between UCS-4 and UTF-8 or vice versa, that will be far more efficient.