문자열을 어떻게 연결합니까?
다음 유형의 조합을 어떻게 연결합니까?
str
과str
String
과str
String
과String
문자열을 연결할 때 결과를 저장하기 위해 메모리를 할당해야합니다. 시작하는 가장 쉬운 방법은 다음 String
과 &str
같습니다.
fn main() {
let mut owned_string: String = "hello ".to_owned();
let borrowed_string: &str = "world";
owned_string.push_str(borrowed_string);
println!("{}", owned_string);
}
여기, 우리는 돌연변이 할 수있는 소유 한 문자열을 가지고 있습니다. 메모리 할당을 재사용 할 수 있으므로 효율적입니다. 거기에 비슷한 경우이다 String
와 String
같이, &String
같은 역 참조 할 수있다&str
.
fn main() {
let mut owned_string: String = "hello ".to_owned();
let another_owned_string: String = "world".to_owned();
owned_string.push_str(&another_owned_string);
println!("{}", owned_string);
}
그 후에 another_owned_string
는 손대지 않습니다 ( mut
예선 자 없음 ). 다른 변형있어 소비 을 String
하지만, 변경할 수를 필요로하지 않습니다는. 이것은 왼쪽과 오른쪽 을 취하는 특성 의 구현입니다Add
.String
&str
fn main() {
let owned_string: String = "hello ".to_owned();
let borrowed_string: &str = "world";
let new_owned_string = owned_string + borrowed_string;
println!("{}", new_owned_string);
}
참고 owned_string
로 호출 한 후 더 이상 액세스 할 수 없습니다 +
.
새로운 줄을 만들고 싶고 둘 다 그대로 두려면 어떻게해야합니까? 가장 간단한 방법은 다음을 사용하는 것입니다 format!
.
fn main() {
let borrowed_string: &str = "hello ";
let another_borrowed_string: &str = "world";
let together = format!("{}{}", borrowed_string, another_borrowed_string);
println!("{}", together);
}
두 입력 변수는 모두 변경할 수 없으므로 터치하지 않습니다. 의 조합에 대해 동일한 작업을 수행하려는 경우 형식을 지정할 수도 String
있다는 사실을 사용할 String
수 있습니다.
fn main() {
let owned_string: String = "hello ".to_owned();
let another_owned_string: String = "world".to_owned();
let together = format!("{}{}", owned_string, another_owned_string);
println!("{}", together);
}
당신은하지 않습니다 이 사용하는 format!
것처럼. 한 문자열을 복제 하고 다른 문자열을 새 문자열에 추가 할 수 있습니다 .
fn main() {
let owned_string: String = "hello ".to_owned();
let borrowed_string: &str = "world";
let together = owned_string.clone() + borrowed_string;
println!("{}", together);
}
참고 -내가 한 모든 유형 사양은 중복입니다. 컴파일러는 여기서 사용되는 모든 유형을 유추 할 수 있습니다. 나는이 질문이 그 그룹에게 인기가 있기를 기대하기 때문에 Rust를 처음 접하는 사람들에게 명확하게 추가했습니다!
To concatenate multiple strings into a single string, separated by another character, there are a couple of ways.
The nicest I have seen is using the join
method on an array:
fn main() {
let a = "Hello";
let b = "world";
let result = [a, b].join("\n");
print!("{}", result);
}
Depending on your use case you might also prefer more control:
fn main() {
let a = "Hello";
let b = "world";
let result = format!("{}\n{}", a, b);
print!("{}", result);
}
There are some more manual ways I have seen, some avoiding one or two allocations here and there. For readability purposes I find the above two to be sufficient.
I think that concat
method and +
should be mentioned here as well:
assert_eq!(
("My".to_owned() + " " + "string"),
["My", " ", "string"].concat()
);
and there is also concat!
macro but only for literals:
let s = concat!("test", 10, 'b', true);
assert_eq!(s, "test10btrue");
참고URL : https://stackoverflow.com/questions/30154541/how-do-i-concatenate-strings
'development' 카테고리의 다른 글
클래스 패스 자원의 java.nio.file.Path (0) | 2020.07.04 |
---|---|
std :: numeric_limits를 호출하기 전에 단항 "+"의 목적은 무엇입니까? (0) | 2020.07.04 |
knockout.js를 ASP.NET MVC ViewModels와 함께 사용하는 방법? (0) | 2020.07.04 |
PostgreSQL이 인덱스 열에서 순차적 스캔을 수행하는 이유는 무엇입니까? (0) | 2020.07.04 |
힘내 : 특정 커밋에 리베이스하는 방법? (0) | 2020.07.04 |